Sha256: 03cad599a3591c7f331b88b479356b56c4b3bdd7655c4409e490be9e503ca5c1

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Retriable
  class ExponentialBackoff
    attr_accessor :tries
    attr_accessor :base_interval
    attr_accessor :multiplier
    attr_accessor :max_interval
    attr_accessor :rand_factor

    def initialize(opts = {})
      @tries         = opts[:tries]         || Retriable.config.tries
      @base_interval = opts[:base_interval] || Retriable.config.base_interval
      @max_interval  = opts[:max_interval]  || Retriable.config.max_interval
      @rand_factor   = opts[:rand_factor]   || Retriable.config.rand_factor
      @multiplier    = opts[:multiplier]    || Retriable.config.multiplier
    end

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

      return intervals if rand_factor == 0

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

    private
    def randomize(interval)
      return interval if rand_factor == 0
      delta = rand_factor * interval * 1.0
      min = interval - delta
      max = interval + delta
      rand(min..max)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
retriable-3.0.0 lib/retriable/exponential_backoff.rb
retriable-2.1.0 lib/retriable/exponential_backoff.rb
retriable-2.0.2 lib/retriable/exponential_backoff.rb
retriable-2.0.1 lib/retriable/exponential_backoff.rb