Class: Rubord::RateLimiter::GlobalLimiter
- Inherits:
-
Object
- Object
- Rubord::RateLimiter::GlobalLimiter
- Defined in:
- lib/rubord/structs/rate_limiter.rb
Instance Method Summary collapse
- #block_for(seconds) ⇒ Object
- #blocked? ⇒ Boolean
-
#initialize ⇒ GlobalLimiter
constructor
A new instance of GlobalLimiter.
- #wait ⇒ Object
Constructor Details
#initialize ⇒ GlobalLimiter
Returns a new instance of GlobalLimiter.
72 73 74 75 |
# File 'lib/rubord/structs/rate_limiter.rb', line 72 def initialize @reset_at = Time.now @mutex = Mutex.new end |
Instance Method Details
#block_for(seconds) ⇒ Object
81 82 83 84 85 |
# File 'lib/rubord/structs/rate_limiter.rb', line 81 def block_for(seconds) @mutex.synchronize do @reset_at = Time.now + seconds end end |
#blocked? ⇒ Boolean
77 78 79 |
# File 'lib/rubord/structs/rate_limiter.rb', line 77 def blocked? @mutex.synchronize { Time.now < @reset_at } end |
#wait ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/rubord/structs/rate_limiter.rb', line 87 def wait @mutex.synchronize do if Time.now < @reset_at sleep_time = @reset_at - Time.now sleep(sleep_time) if sleep_time > 0 end end end |