Module: Throttler

Defined in:
lib/throttler.rb,
lib/throttler/version.rb,
lib/throttler/throttle.rb

Overview

Throttler rate-limits code execution.

Defined Under Namespace

Classes: Throttle

Constant Summary collapse

VERSION =
'0.3.1'

Class Method Summary collapse

Class Method Details

.limit(wait, *words) ⇒ Object

Ensures subsequent code, including any given block, is executed at most per every ‘wait` seconds.

Optionally takes a splat of words to namespace the throttle.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/throttler.rb', line 9

def self.limit(wait, *words)
  words << 'default' if words.empty?
  namespace = words.join '-'

  begin
    throttle = Throttle.new namespace
    throttle.strangle
    throttle.hold wait
  ensure
    throttle.release
  end

  # Syntactic sugar.
  yield if block_given?
end