Class: Parse::Agent::MCPRackApp::PrincipalRateLimiters

Inherits:
Object
  • Object
show all
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

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

Parameters:

Returns:



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