Class: Hatchet::Reaper::ReaperThrottle

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/reaper/reaper_throttle.rb

Overview

This class retains and increments a sleep value between executions

Every time we pause, we increase the duration of the pause 2x. If we do not sleep for long enough then we will burn API requests that we don’t need to make.

To help prevent sleeping for too long, the reaper will sleep for a maximum amount of time equal to the age_sleep_for_ttl. If that happens, it’s likely a fairly large value and the internal incremental value can be reset

Example:

reaper_throttle = ReaperThrottle.new(initial_sleep: 2)
reaper_throttle.call(max_sleep: 5) do |sleep_for|
  puts sleep_for # => 2
end
reaper_throttle.call(max_sleep: 5) do |sleep_for|
  puts sleep_for # => 4
end
reaper_throttle.call(max_sleep: 5) do |sleep_for|
  puts sleep_for # => 5
end

# The throttle is now reset since it hit the max_sleep value

reaper_throttle.call(max_sleep: 5) do |sleep_for|
  puts sleep_for # => 2
end

Instance Method Summary collapse

Constructor Details

#initialize(initial_sleep:) ⇒ ReaperThrottle

Returns a new instance of ReaperThrottle.



31
32
33
34
# File 'lib/hatchet/reaper/reaper_throttle.rb', line 31

def initialize(initial_sleep: )
  @initial_sleep = initial_sleep
  @sleep_for = @initial_sleep
end

Instance Method Details

#call(max_sleep:) {|sleep_for| ... } ⇒ Object

Yields:

  • (sleep_for)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hatchet/reaper/reaper_throttle.rb', line 36

def call(max_sleep: )
  raise "Must call with a block" unless block_given?

  sleep_for = [@sleep_for, max_sleep].min

  yield sleep_for

  if sleep_for < @sleep_for
    reset!
  else
    @sleep_for *= 2
  end
end

#reset!Object



50
51
52
# File 'lib/hatchet/reaper/reaper_throttle.rb', line 50

def reset!
  @sleep_for = @initial_sleep
end