feat(rate-limiter): implement sliding window rate limiting algorithm#187
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
Warning Review limit reached
More reviews will be available in 50 minutes and 23 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR adds a new sliding-window rate-limiting algorithm to the rate-limiter package. The implementation uses dual buckets (current and previous) to efficiently estimate rolling-window request counts with O(1) complexity, supporting both mutating checks and non-mutating peeks. Type definitions, integration logic, and comprehensive tests complete the feature. ChangesSliding-window rate-limiting algorithm
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/rate-limiter/test/algorithms/sliding-window.test.ts (1)
12-17: ⚡ Quick winConsider restoring real timers in
afterEachto prevent test pollution.Adding
vi.useRealTimers()ensures fake timers don't leak into other test files if test order changes.♻️ Suggested addition
beforeEach(() => { storage = createMemoryStorage() vi.useFakeTimers() vi.setSystemTime(0) }) + + afterEach(() => { + vi.useRealTimers() + })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/rate-limiter/test/algorithms/sliding-window.test.ts` around lines 12 - 17, Add an afterEach block to restore real timers to avoid leaking fake timers across tests: after the existing beforeEach that calls vi.useFakeTimers() and vi.setSystemTime(0), add an afterEach that calls vi.useRealTimers() (and optionally resets system time) so the fake timers set up in the beforeEach for these sliding-window tests using createMemoryStorage() are cleaned up after each test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/rate-limiter/src/rate-limiter.ts`:
- Around line 38-39: The resetKeys implementation for the "sliding-window" case
is producing the wrong bucket timestamps and only returning one key; update
resetKeys to compute the current bucket using rule.windowMs (same formula as in
sliding-window.ts, i.e., floor(Date.now() / rule.windowMs) * rule.windowMs) and
also include the previous bucket (currentBucket - rule.windowMs), returning both
keys like `${key}:sw:${currentBucket}` and `${key}:sw:${previousBucket}` so
reset() deletes both buckets.
---
Nitpick comments:
In `@packages/rate-limiter/test/algorithms/sliding-window.test.ts`:
- Around line 12-17: Add an afterEach block to restore real timers to avoid
leaking fake timers across tests: after the existing beforeEach that calls
vi.useFakeTimers() and vi.setSystemTime(0), add an afterEach that calls
vi.useRealTimers() (and optionally resets system time) so the fake timers set up
in the beforeEach for these sliding-window tests using createMemoryStorage() are
cleaned up after each test.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: df52e74e-feb4-494d-b721-1a870d3660d2
📒 Files selected for processing (11)
packages/elysia/package.jsonpackages/express/package.jsonpackages/hono/package.jsonpackages/next/package.jsonpackages/rate-limiter/src/algorithms/index.tspackages/rate-limiter/src/algorithms/sliding-window.tspackages/rate-limiter/src/rate-limiter.tspackages/rate-limiter/src/types.tspackages/rate-limiter/test/algorithms/sliding-window.test.tspackages/react-router/package.jsonpackages/react/package.json
Description
This pull request implements the Sliding Window rate-limiting algorithm.
The algorithm can be used through the centralized
createRateLimiterAPI or directly via the dedicatedcreateSlidingWindowAlgorithmfunction.Unlike the Fixed Window algorithm, Sliding Window evaluates requests over a continuously moving time window, providing more accurate rate limiting and reducing traffic spikes that can occur at window boundaries.
Usage with
createRateLimiterDirect Usage
Features
createRateLimitercreateSlidingWindowAlgorithm