fix(backend): bound rate-limiter IP map (closes #154)#202
Merged
devJaja merged 1 commit intoJul 24, 2026
Conversation
Closes Epta-Node#154. The previous sliding-window rate limiter stored per-IP state in an unbounded `Map<string, Window>` with only a 60-second background TTL sweep. Under an IP-flood attack the map could grow between sweeps and OOM the process. Switch to `lru-cache@^10` as the backing store, which provides both TTL-based eviction (`ttl: windowMs`) and a hard entry cap (`max: maxEntries`, default 10 000) -- so the worst-case memory footprint is bounded regardless of incoming request rate. Behaviour changes: * `RateLimiter.stop()` is now `cache.clear()` rather than `clearInterval()`; the new implementation has no background eviction interval to halt. * `req.ip ?? "unknown"` is unchanged from the original -- applies a global cap until `app.set("trust proxy", ...)` is configured. * `maxEntries` default of 10 000 is per limiter instance; the two module-instantiated default limiters share a global cap of ~20 000 entries. Tests: * Bounded under flood: inserts 25 000 unique IPs against `maxEntries: 10 000`; asserts size() <= 10 000. * LRU policy: with maxEntries 3, after touching 1.1.1.1 the next insert evicts the real LRU (2.2.2.2); 1.1.1.1 keeps its 429 because the cache survived. * TTL: jest.useFakeTimers() advances past windowMs; lru-cache evicts lazily on next access so a returning IP gets a fresh window without a background sweep. Verified: tsc --noEmit clean; middleware tests 11/11; full backend suite 226/226 pass (was 224, +2 new tests for Epta-Node#154).
|
@malaysiaonelove is attempting to deploy a commit to the Jaja's projects Team on Vercel. A member of the Team first needs to authorize it. |
devJaja
self-requested a review
July 24, 2026 20:25
Contributor
|
Nice Implementation @malaysiaonelove LGTM |
6 tasks
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.
Closes #154. Replaces unbounded Map+setInterval rate-limiter with lru-cache@^10 (max+ttl). 226/226 backend tests pass; tsc clean.