Class: Qup::BackoffSleeper

Inherits:
Object
  • Object
show all
Defined in:
lib/qup/backoff_sleeper.rb

Constant Summary collapse

MULTIPLIERS =
[0, 0.01, 0.1, 1]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackoffSleeper

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

#countObject (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

#lengthObject



44
45
46
# File 'lib/qup/backoff_sleeper.rb', line 44

def length
  (multiplier + (multiplier * Kernel.rand)) / 2
end

#multiplierObject



48
49
50
# File 'lib/qup/backoff_sleeper.rb', line 48

def multiplier
  MULTIPLIERS[@count] || MULTIPLIERS.last
end

#resetObject

Reset the backoff sequence to 0.



40
41
42
# File 'lib/qup/backoff_sleeper.rb', line 40

def reset
  @count = 0
end

#tickObject

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