Class: RailsAiBridge::Mcp::CacheRateLimiter
- Inherits:
-
Object
- Object
- RailsAiBridge::Mcp::CacheRateLimiter
- Defined in:
- lib/rails_ai_bridge/mcp/cache_rate_limiter.rb
Overview
Distributed, fixed-window rate limiter backed by Rails.cache (or any
ActiveSupport::Cache::Store). Use it when the MCP HTTP endpoint runs
behind multiple processes and the default in-memory limiter is insufficient.
The limiter relies on the cache's increment operation when available and
falls back to read/write for stores that do not support atomic increments.
Instance Method Summary collapse
-
#allow?(ip) ⇒ Boolean
trueif the request may proceed. -
#initialize(max_requests:, window_seconds:, cache: nil, key_prefix: 'rab:rl') ⇒ CacheRateLimiter
constructor
A new instance of CacheRateLimiter.
Constructor Details
#initialize(max_requests:, window_seconds:, cache: nil, key_prefix: 'rab:rl') ⇒ CacheRateLimiter
Returns a new instance of CacheRateLimiter.
16 17 18 19 20 21 |
# File 'lib/rails_ai_bridge/mcp/cache_rate_limiter.rb', line 16 def initialize(max_requests:, window_seconds:, cache: nil, key_prefix: 'rab:rl') @max_requests = max_requests @window_seconds = window_seconds.to_i @cache = cache @key_prefix = key_prefix end |
Instance Method Details
#allow?(ip) ⇒ Boolean
Returns true if the request may proceed.
25 26 27 28 29 30 31 32 |
# File 'lib/rails_ai_bridge/mcp/cache_rate_limiter.rb', line 25 def allow?(ip) store = cache_store return true unless store key = cache_key(ip) count = increment(store, key) count <= @max_requests end |