Class: Throttle::Instance
- Inherits:
-
Object
- Object
- Throttle::Instance
- Defined in:
- lib/throttle/instance.rb
Instance Method Summary collapse
-
#initialize(strategy, polling, timeout) ⇒ Instance
constructor
A new instance of Instance.
- #limit(&block) ⇒ Object
- #status ⇒ Object
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 |
#status ⇒ Object
27 28 29 |
# File 'lib/throttle/instance.rb', line 27 def status @strategy.status end |