Class: Throttle::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/throttle/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(strategy, polling, timeout) ⇒ Instance

Returns a new instance of Instance.



5
6
7
8
9
# File 'lib/throttle/instance.rb', line 5

def initialize(strategy, polling, timeout)
  @strategy = strategy
  @polling = polling
  @timeout = timeout
end

Instance Method Details

#limit(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/throttle/instance.rb', line 11

def limit(&block)
  timeout(@timeout) do
    loop do
      go, count, time = @strategy.acquire
      if go
        return yield(count, time) if block.arity > 0
        return yield
      end
      sleep @polling
    end
  end

rescue Timeout::Error
  raise Throttle::ThrottledError, "can't execute at this time"
end

#statusObject



27
28
29
# File 'lib/throttle/instance.rb', line 27

def status
  @strategy.status
end