Skip to content

fix(backend): bound rate-limiter IP map (closes #154)#202

Merged
devJaja merged 1 commit into
Epta-Node:mainfrom
malaysiaonelove:fix/154-rate-limiter-bounded
Jul 24, 2026
Merged

fix(backend): bound rate-limiter IP map (closes #154)#202
devJaja merged 1 commit into
Epta-Node:mainfrom
malaysiaonelove:fix/154-rate-limiter-bounded

Conversation

@malaysiaonelove

Copy link
Copy Markdown
Contributor

Closes #154. Replaces unbounded Map+setInterval rate-limiter with lru-cache@^10 (max+ttl). 226/226 backend tests pass; tsc clean.

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).
@vercel

vercel Bot commented Jul 24, 2026

Copy link
Copy Markdown

@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

devJaja commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Nice Implementation @malaysiaonelove

LGTM

@devJaja
devJaja merged commit f7caeb5 into Epta-Node:main Jul 24, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Fix Rate Limiter Unbounded Memory Growth (IP Map Never Evicted)

2 participants