Class: RosettAi::DBus::RateLimiter
- Inherits:
-
Object
- Object
- RosettAi::DBus::RateLimiter
- Defined in:
- lib/rosett_ai/dbus/rate_limiter.rb
Overview
Token bucket rate limiter for D-Bus method calls.
Provides application-level rate limiting to prevent resource exhaustion from rapid D-Bus method invocations. Each method type can have its own rate limit.
Thread-safe: uses mutex for bucket state.
Defined Under Namespace
Classes: RateLimitExceeded
Constant Summary collapse
- DEFAULT_RATE =
Default limits (requests per second / burst capacity)
10- DEFAULT_BURST =
20
Instance Method Summary collapse
-
#allow? ⇒ Boolean
Check if a request is allowed and consume a token if so.
-
#available_tokens ⇒ Float
Current available tokens (for monitoring/debugging).
-
#consume!(method_name = nil) ⇒ Object
Consume a token or raise RateLimitExceeded.
-
#initialize(rate: DEFAULT_RATE, burst: DEFAULT_BURST) ⇒ RateLimiter
constructor
A new instance of RateLimiter.
Constructor Details
#initialize(rate: DEFAULT_RATE, burst: DEFAULT_BURST) ⇒ RateLimiter
Returns a new instance of RateLimiter.
38 39 40 41 42 43 44 |
# File 'lib/rosett_ai/dbus/rate_limiter.rb', line 38 def initialize(rate: DEFAULT_RATE, burst: DEFAULT_BURST) @rate = rate.to_f @burst = burst @tokens = burst.to_f @last_update = Process.clock_gettime(Process::CLOCK_MONOTONIC) @mutex = Mutex.new end |
Instance Method Details
#allow? ⇒ Boolean
Check if a request is allowed and consume a token if so.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rosett_ai/dbus/rate_limiter.rb', line 49 def allow? @mutex.synchronize do refill_tokens if @tokens >= 1.0 @tokens -= 1.0 true else false end end end |
#available_tokens ⇒ Float
Current available tokens (for monitoring/debugging).
72 73 74 75 76 77 |
# File 'lib/rosett_ai/dbus/rate_limiter.rb', line 72 def available_tokens @mutex.synchronize do refill_tokens @tokens end end |
#consume!(method_name = nil) ⇒ Object
Consume a token or raise RateLimitExceeded.
65 66 67 |
# File 'lib/rosett_ai/dbus/rate_limiter.rb', line 65 def consume!(method_name = nil) raise RateLimitExceeded, method_name unless allow? end |