class SimpleThrottle::Manual::Runner

Attributes

remaining[RW]

client can manually override the remaining count.

Public Class Methods

new(wait_s, count, sleep = ->(seconds) { sleep seconds } click to toggle source

Parameters: wait_s: minimum number os seconds between count block invocations count: maximum number of times block may be called between wait_s seconds

# File lib/simple_throttle/manual/runner.rb, line 10
def initialize(wait_s, count, sleep = ->(seconds) { sleep seconds })
  @max = count
  @remaining = @max
  @wait_s = wait_s.to_f
  @sleep = sleep
end

Public Instance Methods

run() { || ... } click to toggle source

Run given block either immediately (if allowed) or after sleep.

# File lib/simple_throttle/manual/runner.rb, line 18
def run(&block)
  if @remaining < 1
    @sleep.call @wait_s
    @remaining = @max
  end

  @remaining -= 1
  yield
end
within_limit?() click to toggle source
# File lib/simple_throttle/manual/runner.rb, line 28
def within_limit?
  @remaining > 0
end