From d280a50bfd4cffc1dd4aaa2731cacbc5ffa0b10c Mon Sep 17 00:00:00 2001 From: strawgate Date: Tue, 12 May 2026 22:40:45 -0500 Subject: [PATCH] docs: clarify LRU eviction safety in rate limiter docstrings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../fastmcp/server/middleware/rate_limiting.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py b/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py index dbc872fc6..0a44469b4 100644 --- a/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py +++ b/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py @@ -145,7 +145,13 @@ class RateLimitingMiddleware(Middleware): ) def _get_limiter(self, client_id: str) -> TokenBucketRateLimiter: - """Get or create a rate limiter for a client, with LRU eviction.""" + """Get or create a rate limiter for a client, with LRU eviction. + + When the cache is full, the least-recently-used client is evicted. + This is safe because an evicted client was inactive long enough for + max_clients other clients to be more recent — by which point their + token bucket would have refilled to capacity anyway. + """ if client_id in self._client_limiters: self._client_limiters.move_to_end(client_id) return self._client_limiters[client_id] @@ -229,7 +235,13 @@ class SlidingWindowRateLimitingMiddleware(Middleware): ) def _get_limiter(self, client_id: str) -> SlidingWindowRateLimiter: - """Get or create a rate limiter for a client, with LRU eviction.""" + """Get or create a rate limiter for a client, with LRU eviction. + + When the cache is full, the least-recently-used client is evicted. + This is safe because an evicted client was inactive long enough for + max_clients other clients to be more recent — by which point their + sliding window would have expired anyway. + """ if client_id in self._client_limiters: self._client_limiters.move_to_end(client_id) return self._client_limiters[client_id]