Class: Fizzy::RateLimiter
- Inherits:
-
Object
- Object
- Fizzy::RateLimiter
- Defined in:
- lib/fizzy/rate_limiter.rb
Overview
Token bucket rate limiter for client-side rate limiting.
Prevents the client from exceeding a configurable request rate, avoiding 429 responses from the server.
Instance Method Summary collapse
-
#acquire ⇒ void
Acquires a token, blocking if necessary until one is available.
-
#initialize(rate: 10, burst: 20) ⇒ RateLimiter
constructor
A new instance of RateLimiter.
-
#try_acquire ⇒ Boolean
Attempts to acquire a token without blocking.
Constructor Details
#initialize(rate: 10, burst: 20) ⇒ RateLimiter
Returns a new instance of RateLimiter.
15 16 17 18 19 20 21 |
# File 'lib/fizzy/rate_limiter.rb', line 15 def initialize(rate: 10, burst: 20) @rate = rate.to_f @burst = burst @tokens = burst.to_f @last_refill = Process.clock_gettime(Process::CLOCK_MONOTONIC) @mutex = Mutex.new end |
Instance Method Details
#acquire ⇒ void
This method returns an undefined value.
Acquires a token, blocking if necessary until one is available.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fizzy/rate_limiter.rb', line 26 def acquire loop do wait_time = nil @mutex.synchronize do refill if @tokens >= 1.0 @tokens -= 1.0 return else wait_time = (1.0 - @tokens) / @rate end end sleep(wait_time) if wait_time&.positive? end end |
#try_acquire ⇒ Boolean
Attempts to acquire a token without blocking.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fizzy/rate_limiter.rb', line 47 def try_acquire @mutex.synchronize do refill if @tokens >= 1.0 @tokens -= 1.0 true else false end end end |