Class: R2::Retry::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/r2/retry.rb

Overview

Retry settings shared by the operations of a client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: DEFAULT_MAX_ATTEMPTS, base_delay: DEFAULT_BASE_DELAY, max_delay: DEFAULT_MAX_DELAY) ⇒ Policy

Returns a new instance of Policy.

Parameters:

  • max_attempts (Integer) (defaults to: DEFAULT_MAX_ATTEMPTS) —

    total number of attempts

  • base_delay (Float) (defaults to: DEFAULT_BASE_DELAY) —

    wait after the first failure, in seconds

  • max_delay (Float) (defaults to: DEFAULT_MAX_DELAY) —

    upper limit of the wait, in seconds



36
37
38
39
40
41
42
43
44
# File 'lib/r2/retry.rb', line 36

def initialize(
    max_attempts: DEFAULT_MAX_ATTEMPTS,
    base_delay: DEFAULT_BASE_DELAY,
    max_delay: DEFAULT_MAX_DELAY
)
    @max_attempts = max_attempts
    @base_delay = base_delay
    @max_delay = max_delay
end

Instance Attribute Details

#base_delay ⇒ Object (readonly)

Returns the value of attribute base_delay.



31
32
33
# File 'lib/r2/retry.rb', line 31

def base_delay
  @base_delay
end

#max_attempts ⇒ Object (readonly)

Returns the value of attribute max_attempts.



31
32
33
# File 'lib/r2/retry.rb', line 31

def max_attempts
  @max_attempts
end

#max_delay ⇒ Object (readonly)

Returns the value of attribute max_delay.



31
32
33
# File 'lib/r2/retry.rb', line 31

def max_delay
  @max_delay
end

Instance Method Details

#delay_for(attempt) ⇒ Float

Wait applied after the given failed attempt.

Parameters:

  • attempt (Integer) —

    number of the attempt that just failed

Returns:

  • (Float) —

    seconds to wait before the next attempt



50
51
52
# File 'lib/r2/retry.rb', line 50

def delay_for(attempt)
    [@base_delay * (2**(attempt - 1)), @max_delay].min
end