Class: Qup::BackoffSleeper
- Inherits:
-
Object
- Object
- Qup::BackoffSleeper
- Defined in:
- lib/qup/backoff_sleeper.rb
Constant Summary collapse
- MULTIPLIERS =
[0, 0.01, 0.1, 1]
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
-
#initialize ⇒ BackoffSleeper
constructor
A new instance of BackoffSleeper.
- #length ⇒ Object
- #multiplier ⇒ Object
-
#reset ⇒ Object
Reset the backoff sequence to 0.
-
#tick ⇒ Object
Register an iteration.
Constructor Details
#initialize ⇒ BackoffSleeper
Returns a new instance of BackoffSleeper.
28 29 30 |
# File 'lib/qup/backoff_sleeper.rb', line 28 def initialize @count = 0 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
24 25 26 |
# File 'lib/qup/backoff_sleeper.rb', line 24 def count @count end |
Instance Method Details
#length ⇒ Object
44 45 46 |
# File 'lib/qup/backoff_sleeper.rb', line 44 def length (multiplier + (multiplier * Kernel.rand)) / 2 end |
#multiplier ⇒ Object
48 49 50 |
# File 'lib/qup/backoff_sleeper.rb', line 48 def multiplier MULTIPLIERS[@count] || MULTIPLIERS.last end |
#reset ⇒ Object
Reset the backoff sequence to 0.
40 41 42 |
# File 'lib/qup/backoff_sleeper.rb', line 40 def reset @count = 0 end |
#tick ⇒ Object
Register an iteration. Sleeps if neccesary. Does not sleep if #reset has just been called.
34 35 36 37 |
# File 'lib/qup/backoff_sleeper.rb', line 34 def tick Kernel.sleep(length) if length > 0 @count += 1 end |