Class: StagedEvent::BackoffTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/staged_event/backoff_timer.rb

Constant Summary collapse

INCREMENTS_BEFORE_BACKOFF =
5
MAX_VALUE =
25

Instance Method Summary collapse

Constructor Details

#initializeBackoffTimer

Returns a new instance of BackoffTimer.



8
9
10
# File 'lib/staged_event/backoff_timer.rb', line 8

def initialize
  reset
end

Instance Method Details

#incrementObject



12
13
14
# File 'lib/staged_event/backoff_timer.rb', line 12

def increment
  @increments += 1
end

#resetObject



16
17
18
# File 'lib/staged_event/backoff_timer.rb', line 16

def reset
  @increments = 0
end

#valueObject



20
21
22
23
24
25
# File 'lib/staged_event/backoff_timer.rb', line 20

def value
  # returns 1 until the timer has been incremented n times, after which it
  # returns n^2 (until reaching a set maximum)
  backoff_time = [ 1, increments - INCREMENTS_BEFORE_BACKOFF + 1 ].max**2
  [ backoff_time, MAX_VALUE ].min
end