Skip to content

fix: bound and evict stale buckets in in-memory rate limiter (Closes #299) - #398

Open
TheWeirdDee wants to merge 1 commit into
Adamantine-guild:mainfrom
TheWeirdDee:fix/bound-rate-limit-buckets
Open

fix: bound and evict stale buckets in in-memory rate limiter (Closes #299)#398
TheWeirdDee wants to merge 1 commit into
Adamantine-guild:mainfrom
TheWeirdDee:fix/bound-rate-limit-buckets

Conversation

@TheWeirdDee

Copy link
Copy Markdown

Summary of Changes

Fixes unbounded memory growth in \lib/rate-limit.ts\ by introducing bounded bucket eviction (idle pruning + LRU capacity cap) to \InMemoryRateLimitStore.

  • Idle Bucket Pruning: Buckets whose tokens have fully refilled and have been inactive for at least \idleTimeoutMs\ (defaults to 60s) are automatically pruned on new bucket creation or capacity checks.
  • LRU Capacity Cap: Enforces a \maxBuckets\ capacity cap (defaults to 10,000) using Least-Recently-Used (LRU) eviction when active keys reach capacity under high volume or header spoofing attacks.
  • LRU Order Maintenance: Active bucket lookups update \lastAccess\ timestamps and refresh insertion order in JS \Map\ to guarantee true LRU eviction.
  • Test Introspection & Isolation: Exported
    esetRateLimitStateForTest()\ and \getRateLimitBucketCountForTest(store)\ helpers.
  • Expanded Unit Tests: Added unit tests in \ est/rate-limit.test.ts\ verifying idle eviction, LRU cap eviction, returning evicted keys, and store introspection.
  • Documentation: Updated \docs/deployment.md\ single-instance caveat to document the eviction policy.

Closes #299

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.

Bound and evict stale buckets in the in-memory rate limiter

1 participant