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



15
16
17
# File 'lib/unique_thread/stopwatch.rb', line 15

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



19
20
21
22
23
# File 'lib/unique_thread/stopwatch.rb', line 19

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



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

def sleep_until_renewal_attempt
  Kernel.sleep(downtime / 3)
end