Class: Tootsie::Utility::Backoff

Inherits:
Object
  • Object
show all
Defined in:
lib/tootsie/utility/backoff.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Backoff

Returns a new instance of Backoff.



6
7
8
9
10
11
# File 'lib/tootsie/utility/backoff.rb', line 6

def initialize(options = {})
  @min = options[:min] || 0.5
  @max = options[:max] || 2.0
  @current = options[:initial] || @min
  @factor = options[:factor] || 1.5
end

Instance Method Details

#with(&block) ⇒ Object



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

def with(&block)
  loop do
    result = yield
    if result
      @current /= @factor
      return result
    else
      @current = [[@current * @factor, @min].max, @max].min
      sleep(@current) if @current > 0
    end
  end
end