Class: Nfe::Http::RetryPolicy

Inherits:
Data
  • Object
show all
Defined in:
lib/nfe/http/retry_policy.rb,
sig/nfe/http/retry_policy.rbs

Overview

Immutable retry policy describing exponential backoff with symmetric jitter. Consumed by RetryingTransport to decide how long to wait between attempts.

max_retries is the number of retries after the initial attempt, so a policy with max_retries: 3 makes at most four HTTP attempts.

Use the RetryPolicy.default factory for the recommended settings, or RetryPolicy.none to disable retries entirely (exactly one HTTP attempt).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRetryPolicy

Returns a new instance of RetryPolicy.

Parameters:

  • max_retries: (Integer)
  • base_delay: (Float)
  • max_delay: (Float)
  • jitter: (Float)


10
# File 'sig/nfe/http/retry_policy.rbs', line 10

def initialize: (max_retries: Integer, base_delay: Float, max_delay: Float, jitter: Float) -> void

Instance Attribute Details

#base_delayFloat (readonly)

Returns the value of attribute base_delay.

Returns:

  • (Float)


5
6
7
# File 'sig/nfe/http/retry_policy.rbs', line 5

def base_delay
  @base_delay
end

#jitterFloat (readonly)

Returns the value of attribute jitter.

Returns:

  • (Float)


7
8
9
# File 'sig/nfe/http/retry_policy.rbs', line 7

def jitter
  @jitter
end

#max_delayFloat (readonly)

Returns the value of attribute max_delay.

Returns:

  • (Float)


6
7
8
# File 'sig/nfe/http/retry_policy.rbs', line 6

def max_delay
  @max_delay
end

#max_retriesInteger (readonly)

Returns the value of attribute max_retries.

Returns:

  • (Integer)


4
5
6
# File 'sig/nfe/http/retry_policy.rbs', line 4

def max_retries
  @max_retries
end

Class Method Details

.defaultRetryPolicy

Recommended defaults: 3 retries, 1s base, 30s cap, +/-30% jitter.

Returns:



16
17
18
# File 'lib/nfe/http/retry_policy.rb', line 16

def self.default
  new(max_retries: 3, base_delay: 1.0, max_delay: 30.0, jitter: 0.3)
end

.newRetryPolicy

Parameters:

  • max_retries: (Integer)
  • base_delay: (Float)
  • max_delay: (Float)
  • jitter: (Float)

Returns:



9
# File 'sig/nfe/http/retry_policy.rbs', line 9

def self.new: (max_retries: Integer, base_delay: Float, max_delay: Float, jitter: Float) -> RetryPolicy

.noneRetryPolicy

No retries: exactly one HTTP attempt, zero delay.

Returns:



21
22
23
# File 'lib/nfe/http/retry_policy.rb', line 21

def self.none
  new(max_retries: 0, base_delay: 0.0, max_delay: 0.0, jitter: 0.0)
end

Instance Method Details

#delay_for(attempt) ⇒ Float

Delay in seconds before the given (1-based) retry attempt.

Computes min(max_delay, base_delay * 2**(attempt - 1)) then applies symmetric jitter, with the final value capped at max_delay.

Parameters:

  • attempt (Integer)

    1-based retry index.

Returns:

  • (Float)


32
33
34
35
36
# File 'lib/nfe/http/retry_policy.rb', line 32

def delay_for(attempt)
  base = [base_delay * (2**(attempt - 1)), max_delay].min
  jittered = base * (1 - jitter + (2 * jitter * rand))
  [jittered, max_delay].min
end