Class: Chronos::Application::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/application/retry_policy.rb

Overview

Bounded retry timing policy with exponential backoff and jitter.

Examples:

policy.delay(2, result)

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



18
19
20
21
22
23
24
# File 'lib/chronos/application/retry_policy.rb', line 18

def initialize(options)
  @max_retries = options[:max_retries]
  @base_interval = options[:base_interval]
  @max_interval = options[:max_interval]
  @jitter = options[:jitter]
  @random = options[:random] || proc { rand }
end

Instance Method Details

#delay(retry_number, result = nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/chronos/application/retry_policy.rb', line 30

def delay(retry_number, result = nil)
  retry_after = retry_after_seconds(result)
  return [retry_after, @max_interval].min if retry_after

  base = @base_interval * (2**(retry_number - 1))
  jittered = base * (1.0 + (@random.call.to_f * @jitter))
  [jittered, @max_interval].min
end

#retry?(result, retries) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/chronos/application/retry_policy.rb', line 26

def retry?(result, retries)
  result.retryable? && retries < @max_retries
end