Class: Sendmux::Core::RetryOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/sendmux/core/retry_options.rb

Constant Summary collapse

RETRY_STATUSES =
[408, 409, 425, 429, 500, 502, 503, 504].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: 3, base_delay_seconds: 0.25, max_delay_seconds: 5.0, jitter: true) ⇒ RetryOptions

Returns a new instance of RetryOptions.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sendmux/core/retry_options.rb', line 10

def initialize(max_attempts: 3, base_delay_seconds: 0.25, max_delay_seconds: 5.0, jitter: true)
  raise ArgumentError, 'max_attempts must be at least 1' if max_attempts < 1
  if base_delay_seconds.negative? || max_delay_seconds.negative?
    raise ArgumentError,
          'retry delays must be non-negative'
  end

  @max_attempts = max_attempts
  @base_delay_seconds = base_delay_seconds
  @max_delay_seconds = max_delay_seconds
  @jitter = jitter
end

Instance Attribute Details

#base_delay_secondsObject (readonly)

Returns the value of attribute base_delay_seconds.



8
9
10
# File 'lib/sendmux/core/retry_options.rb', line 8

def base_delay_seconds
  @base_delay_seconds
end

#jitterObject (readonly)

Returns the value of attribute jitter.



8
9
10
# File 'lib/sendmux/core/retry_options.rb', line 8

def jitter
  @jitter
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



8
9
10
# File 'lib/sendmux/core/retry_options.rb', line 8

def max_attempts
  @max_attempts
end

#max_delay_secondsObject (readonly)

Returns the value of attribute max_delay_seconds.



8
9
10
# File 'lib/sendmux/core/retry_options.rb', line 8

def max_delay_seconds
  @max_delay_seconds
end

Instance Method Details

#to_faraday_optionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sendmux/core/retry_options.rb', line 23

def to_faraday_options
  {
    max: max_attempts - 1,
    interval: base_delay_seconds,
    max_interval: max_delay_seconds,
    interval_randomness: jitter ? 0.5 : 0.0,
    backoff_factor: 2,
    methods: [],
    retry_statuses: RETRY_STATUSES,
    rate_limit_retry_header: 'Retry-After',
    rate_limit_reset_header: 'X-RateLimit-Reset',
    retry_if: ->(env, _exception) { Retry.retryable_request?(env) }
  }
end