Class: Retriable::ExponentialBackoff

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

Constant Summary collapse

ATTRIBUTES =
[
  :tries,
  :base_interval,
  :multiplier,
  :max_interval,
  :rand_factor,
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ExponentialBackoff

Returns a new instance of ExponentialBackoff.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/retriable/exponential_backoff.rb', line 13

def initialize(opts = {})
  @tries         = 3
  @base_interval = 0.5
  @max_interval  = 60
  @rand_factor   = 0.5
  @multiplier    = 1.5

  opts.each do |k, v|
    raise ArgumentError, "#{k} is not a valid option" if !ATTRIBUTES.include?(k)
    instance_variable_set(:"@#{k}", v)
  end
end

Instance Method Details

#intervalsObject



26
27
28
29
30
31
32
33
34
# File 'lib/retriable/exponential_backoff.rb', line 26

def intervals
  intervals = Array.new(tries) do |iteration|
    [base_interval * multiplier**iteration, max_interval].min
  end

  return intervals if rand_factor.zero?

  intervals.map { |i| randomize(i) }
end