Class: Parse::Agent::MCPRackApp::PrincipalRateLimiters
- Inherits:
-
Object
- Object
- Parse::Agent::MCPRackApp::PrincipalRateLimiters
- Defined in:
- lib/parse/agent/mcp_deployments.rb
Overview
One rate limiter per principal, shared by every agent a deployment factory builds for that principal. A factory builds a fresh agent per request, and each agent would otherwise get a fresh limiter, so the configured rate limit never accumulated across requests. Bounded (least recently used) so a stream of principals cannot grow it without limit; an evicted principal starts a new window.
Constant Summary collapse
- DEFAULT_MAX_ENTRIES =
10_000
Instance Method Summary collapse
- #fetch(principal) ⇒ Parse::Agent::RateLimiter
-
#initialize(limit:, window:, max_entries: DEFAULT_MAX_ENTRIES) ⇒ PrincipalRateLimiters
constructor
A new instance of PrincipalRateLimiters.
- #size ⇒ Object
Constructor Details
#initialize(limit:, window:, max_entries: DEFAULT_MAX_ENTRIES) ⇒ PrincipalRateLimiters
Returns a new instance of PrincipalRateLimiters.
54 55 56 57 58 59 60 |
# File 'lib/parse/agent/mcp_deployments.rb', line 54 def initialize(limit:, window:, max_entries: DEFAULT_MAX_ENTRIES) @limit = limit @window = window @max = max_entries @limiters = {} @mutex = Mutex.new end |
Instance Method Details
#fetch(principal) ⇒ Parse::Agent::RateLimiter
64 65 66 67 68 69 70 71 72 |
# File 'lib/parse/agent/mcp_deployments.rb', line 64 def fetch(principal) key = principal.to_s @mutex.synchronize do limiter = @limiters.delete(key) || Parse::Agent::RateLimiter.new(limit: @limit, window: @window) @limiters[key] = limiter @limiters.shift while @limiters.size > @max limiter end end |
#size ⇒ Object
74 75 76 |
# File 'lib/parse/agent/mcp_deployments.rb', line 74 def size @mutex.synchronize { @limiters.size } end |