Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(api): fallback to soonest-expiring eviction when all entries are rate-limited

Minit83a0eee2916b
diff --git a/apps/web/app/api/utils.ts b/apps/web/app/api/utils.ts
index 7d992dbb4..0eb33aa57 100644
--- a/apps/web/app/api/utils.ts
+++ b/apps/web/app/api/utils.ts
@@ -38,6 +38,15 @@ export const developerRateLimiter = createMiddleware(async (c, next) => {
if (requestCounts.size <= RATE_LIMIT_MAX_ENTRIES) break;
if (v.count < RATE_LIMIT_MAX_REQUESTS) requestCounts.delete(k);
}
+ if (requestCounts.size > RATE_LIMIT_MAX_ENTRIES) {
+ const byExpiry = [...requestCounts.entries()].sort(
+ (a, b) => a[1].resetAt - b[1].resetAt,
+ );
+ for (const [k] of byExpiry) {
+ if (requestCounts.size <= RATE_LIMIT_MAX_ENTRIES) break;
+ requestCounts.delete(k);
+ }
+ }
}
}