Fix rate limiter memory leak and update defaults#9
Merged
Conversation
…e defaults The rate limit registry previously grew unbounded — every unique IP that hit the server got a permanent map entry with no eviction. On public-facing services this is an unbounded memory growth vector. Changes: - Add rateLimitEntry with lastSeen timestamp per key - Add background cleanup goroutine that sweeps stale entries - Add CleanupInterval (default 1m) and MaxIdleTime (default 5m) options - Update defaults from 10 req/s burst 20 to 15 req/s burst 30 - Add registry.close() to stop the cleanup goroutine - Add registry.size() for testability - Add comprehensive internal tests for eviction behavior - Maintain 100% middleware coverage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem
The
rateLimitRegistrystored amap[string]*rate.Limiterthat grew unbounded — every unique IP that hit the server got a permanent entry with no eviction. On a public-facing server seeing 100K+ unique IPs per day, this is a slow memory leak (~20MB/day) that only resets on process restart.Solution
TTL Eviction
lastSeentimestamp, updated on every accessMaxIdleTime(default: 5 minutes) are deletedregistry.close()stops the goroutine cleanlyUpdated Defaults
New Options
CleanupInterval(default 1m) — how often the cleanup goroutine runsMaxIdleTime(default 5m) — how long an entry can be idle before evictionTesting
framework/middlewareremains at 100% coveragego test ./contract/... ./router/... ./problem/... ./framework/...