Class: Smith::ExponentialBackoff

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/exponential_backoff.rb

Constant Summary collapse

MAX_SLEEP_INTERVAL_SECONDS =
2_147_483_647.0
MAX_RANDOM_FACTOR =
1.0.prev_float

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attempts:, base_delay:, max_delay:, jitter:, delay_label: "base_delay") ⇒ ExponentialBackoff

Returns a new instance of ExponentialBackoff.



10
11
12
13
14
15
16
17
18
# File 'lib/smith/exponential_backoff.rb', line 10

def initialize(attempts:, base_delay:, max_delay:, jitter:, delay_label: "base_delay")
  @attempt_limit = Smith.config.retry_attempt_limit
  @attempts = validate_attempts!(attempts)
  @base_delay = normalize_delay!(base_delay, delay_label)
  @max_delay = normalize_optional_delay!(max_delay, "max_delay")
  @jitter = normalize_delay!(jitter, "jitter")
  validate_finite_schedule!
  freeze
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



8
9
10
# File 'lib/smith/exponential_backoff.rb', line 8

def attempts
  @attempts
end

#base_delayObject (readonly)

Returns the value of attribute base_delay.



8
9
10
# File 'lib/smith/exponential_backoff.rb', line 8

def base_delay
  @base_delay
end

#jitterObject (readonly)

Returns the value of attribute jitter.



8
9
10
# File 'lib/smith/exponential_backoff.rb', line 8

def jitter
  @jitter
end

#max_delayObject (readonly)

Returns the value of attribute max_delay.



8
9
10
# File 'lib/smith/exponential_backoff.rb', line 8

def max_delay
  @max_delay
end

Instance Method Details

#delay(failed_attempt, random: Kernel.method(:rand)) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/smith/exponential_backoff.rb', line 20

def delay(failed_attempt, random: Kernel.method(:rand))
  validate_failed_attempt!(failed_attempt)

  base = exponential_delay(failed_attempt - 1)
  return base if jitter.zero? || max_delay == base

  add_jitter(base, random.call)
end