Class: Tootsie::Utility::Backoff
- Inherits:
-
Object
- Object
- Tootsie::Utility::Backoff
- Defined in:
- lib/tootsie/utility/backoff.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Backoff
constructor
A new instance of Backoff.
- #with(&block) ⇒ Object
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( = {}) @min = [:min] || 0.5 @max = [:max] || 2.0 @current = [:initial] || @min @factor = [: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 |