Sha256: 507c5a12c310b895432ab78577a20d00d82cbb3d03efb7a92fd42d4ebcb02f79

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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(
        tries:            Retriable.config.tries,
        base_interval:    Retriable.config.base_interval,
        multiplier:       Retriable.config.multiplier,
        max_interval:     Retriable.config.max_interval,
        rand_factor:      Retriable.config.rand_factor
      )

      @tries          = tries
      @base_interval  = base_interval
      @multiplier     = multiplier
      @max_interval   = max_interval
      @rand_factor    = rand_factor
    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

1 entries across 1 versions & 1 rubygems

Version Path
retriable-2.0.0 lib/retriable/exponential_backoff.rb