Class: Clerk::Utils::RetryConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/clerk/utils/retries.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backoff: nil, retry_connection_errors: nil, strategy: nil) ⇒ RetryConfig

Returns a new instance of RetryConfig.



48
49
50
51
52
# File 'lib/clerk/utils/retries.rb', line 48

def initialize(backoff: nil, retry_connection_errors: nil, strategy: nil)
  @backoff = backoff
  @retry_connection_errors = retry_connection_errors
  @strategy = strategy
end

Instance Attribute Details

#backoffObject

Returns the value of attribute backoff.



39
40
41
# File 'lib/clerk/utils/retries.rb', line 39

def backoff
  @backoff
end

#retry_connection_errorsObject

Returns the value of attribute retry_connection_errors.



42
43
44
# File 'lib/clerk/utils/retries.rb', line 42

def retry_connection_errors
  @retry_connection_errors
end

#strategyObject

Returns the value of attribute strategy.



45
46
47
# File 'lib/clerk/utils/retries.rb', line 45

def strategy
  @strategy
end

Instance Method Details

#to_faraday_retry_options(initial_time:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clerk/utils/retries.rb', line 55

def to_faraday_retry_options(initial_time:)
  retry_options = {
    # must overwrite default max of 2 retries and it must be positive
    max: 1_000_000_000,
    # ensure all HTTP methods are retried, especially via retry_if
    methods: [],
  }

  if @retry_connection_errors
    retry_options[:exceptions] = Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS + [Faraday::ConnectionFailed]
  end

  if @strategy == 'backoff' && @backoff
    retry_options[:backoff_factor] = @backoff.exponent unless @backoff.exponent.nil?
    retry_options[:interval] = (@backoff.initial_interval.to_f / 1000) unless @backoff.initial_interval.nil?
    retry_options[:max_interval] = @backoff.max_interval unless @backoff.max_interval.nil?

    unless @backoff.max_elapsed_time.nil?
      stop_time = initial_time + (@backoff.max_elapsed_time.to_f / 1000)
      retry_options[:retry_if] = ->(_env, _exc) { Time.now < stop_time }
    end
  end

  retry_options
end