Class: UniqueThread::Stopwatch
- Inherits:
-
Object
- Object
- UniqueThread::Stopwatch
- Defined in:
- lib/unique_thread/stopwatch.rb
Instance Attribute Summary collapse
-
#downtime ⇒ Object
readonly
Returns the value of attribute downtime.
Instance Method Summary collapse
-
#initialize(downtime:) ⇒ Stopwatch
constructor
A new instance of Stopwatch.
-
#next_renewal ⇒ Object
FIXME: Bad name.
- #now ⇒ Object
- #sleep_until_next_attempt(locked_until) ⇒ Object
- #sleep_until_renewal_attempt ⇒ Object
Constructor Details
#initialize(downtime:) ⇒ Stopwatch
Returns a new instance of Stopwatch.
7 8 9 |
# File 'lib/unique_thread/stopwatch.rb', line 7 def initialize(downtime:) @downtime = downtime.to_f end |
Instance Attribute Details
#downtime ⇒ Object (readonly)
Returns the value of attribute downtime.
5 6 7 |
# File 'lib/unique_thread/stopwatch.rb', line 5 def downtime @downtime end |
Instance Method Details
#next_renewal ⇒ Object
FIXME: Bad name. Lap maybe? Milestone?
16 17 18 |
# File 'lib/unique_thread/stopwatch.rb', line 16 def next_renewal now + (downtime * 2 / 3) end |
#now ⇒ Object
11 12 13 |
# File 'lib/unique_thread/stopwatch.rb', line 11 def now Time.now.to_f end |
#sleep_until_next_attempt(locked_until) ⇒ Object
20 21 22 23 24 |
# File 'lib/unique_thread/stopwatch.rb', line 20 def sleep_until_next_attempt(locked_until) seconds_until_next_attempt = [locked_until - now + Random.new.rand(downtime / 3), 0].max Kernel.sleep(seconds_until_next_attempt) end |
#sleep_until_renewal_attempt ⇒ Object
26 27 28 |
# File 'lib/unique_thread/stopwatch.rb', line 26 def sleep_until_renewal_attempt Kernel.sleep(downtime / 3) end |