Skip to content

Commit e3426ad

Browse files
Merge pull request #1736 from MinitJain/fix/rate-limiter-clear
fix(api): evict only non-rate-limited entries when rate limiter map exceeds capacity
2 parents c8b86c5 + 221ee65 commit e3426ad

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎apps/web/app/api/utils.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@ export const developerRateLimiter = createMiddleware(async (c, next) => {
3535
if (now > v.resetAt) requestCounts.delete(k);
3636
}
3737
if (requestCounts.size > RATE_LIMIT_MAX_ENTRIES) {
38-
requestCounts.clear();
38+
for (const [k, v] of requestCounts) {
39+
if (requestCounts.size <= RATE_LIMIT_MAX_ENTRIES) break;
40+
if (v.count < RATE_LIMIT_MAX_REQUESTS) requestCounts.delete(k);
41+
}
42+
if (requestCounts.size > RATE_LIMIT_MAX_ENTRIES) {
43+
const byExpiry = [...requestCounts.entries()].sort(
44+
(a, b) => a[1].resetAt - b[1].resetAt,
45+
);
46+
for (const [k] of byExpiry) {
47+
if (requestCounts.size <= RATE_LIMIT_MAX_ENTRIES) break;
48+
requestCounts.delete(k);
49+
}
50+
}
3951
}
4052
}
4153

0 commit comments

Comments
 (0)