fix(rate-limiter): add periodic cleanup to prevent memory leak - #52
Merged
Conversation
- Add sweep() method that removes stale keys (no timestamps within maxWindowMs) - Add startCleanup() with configurable interval (default: 5 min) - Add destroy() for graceful shutdown - Remove empty keys in check() to prevent unbounded Map growth - Add constructor parameters: cleanupIntervalMs, maxWindowMs Memory safety: entries are cleaned up in two ways: 1. Per-key pruning — expired timestamps removed on every check() call 2. Global sweep — periodic interval removes stale keys Co-Authored-By: OWL <noreply@anthropic.com>
…test failures Background setInterval was interfering with vitest fake timers, causing 4/5 tests to fail. Replaced with: - Incremental sweep every N check() calls (default: 100) - Optional background interval (can be disabled with cleanupIntervalMs=0) - sweep() accepts 'now' parameter for deterministic testing Co-Authored-By: OWL <noreply@anthropic.com>
Removed background setInterval that was interfering with vitest fake timers and causing 4/5 tests to fail. Now uses only incremental sweep every N check() calls (default: 100) with a 5-minute stale cutoff. Co-Authored-By: OWL <noreply@anthropic.com>
The delete(key) call was removing the Map entry before the new timestamp was pushed, causing the array to be orphaned from the Map. Removed the premature delete — keys are now only cleaned up by sweepStaleKeys(). Co-Authored-By: OWL <noreply@anthropic.com>
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
Fixes memory leak in PluginRateLimiter — the internal windows Map never removed keys whose timestamps had all expired, causing unbounded growth with many unique plugin/action combinations.
Changes
Memory Safety
Entries are cleaned up in two ways:
Testing
Existing tests continue to pass.
Fixes #46
Checklist