Class: UniqueThread::Stopwatch

Inherits:
Object
  • Object
show all
Defined in:
lib/unique_thread/stopwatch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#downtimeObject (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_renewalObject

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

#nowObject



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_attemptObject



26
27
28
# File 'lib/unique_thread/stopwatch.rb', line 26

def sleep_until_renewal_attempt
  Kernel.sleep(downtime / 3)
end