detached fe68614

fix(rate-limiter): use max duration for cleanup

commit
fe686143cfdc528cdf481a4b2a8ec39a27b0127d
author
bnema · · 2026-01-18 21:47 UTC
parent
7119e85
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
1 file · +12 -3
M src/lib/api/rate-limiter.ts +12 -3
@@ -41,17 +41,26 @@ const RATE_LIMITS = {
4141 // In-memory store for rate limiting
4242 const ipRequests = new Map<string, Array<{ timestamp: number }>>();
4343
44+// Compute max duration across all rate limits for cleanup
45+const MAX_RATE_LIMIT_DURATION = Math.max(
46+ RATE_LIMITS.auth.durationMs,
47+ RATE_LIMITS.public.durationMs,
48+ RATE_LIMITS.protected.durationMs,
49+ RATE_LIMITS.sse.authenticated.durationMs,
50+ RATE_LIMITS.sse.unauthenticated.durationMs
51+);
52+
4453 // Lazy cleanup: remove stale entries on each check (Cloudflare Workers don't allow setInterval at global scope)
4554 let lastCleanup = 0;
46-const CLEANUP_INTERVAL = 5 * 60 * 1000; // 5 minutes
4755
4856 function cleanupStaleEntries() {
4957 const now = Date.now();
50- if (now - lastCleanup < CLEANUP_INTERVAL) return;
58+ // Run cleanup at most once per max duration window
59+ if (now - lastCleanup < MAX_RATE_LIMIT_DURATION) return;
5160 lastCleanup = now;
5261
5362 for (const [ip, timestamps] of ipRequests.entries()) {
54- const filtered = timestamps.filter((t) => now - t.timestamp < CLEANUP_INTERVAL);
63+ const filtered = timestamps.filter((t) => now - t.timestamp < MAX_RATE_LIMIT_DURATION);
5564 if (filtered.length === 0) {
5665 ipRequests.delete(ip);
5766 } else {
@@ -41,17 +41,26 @@ const RATE_LIMITS = {
41 // In-memory store for rate limiting41 // In-memory store for rate limiting
42 const ipRequests = new Map<string, Array<{ timestamp: number }>>();42 const ipRequests = new Map<string, Array<{ timestamp: number }>>();
43 43
44+// Compute max duration across all rate limits for cleanup
45+const MAX_RATE_LIMIT_DURATION = Math.max(
46+ RATE_LIMITS.auth.durationMs,
47+ RATE_LIMITS.public.durationMs,
48+ RATE_LIMITS.protected.durationMs,
49+ RATE_LIMITS.sse.authenticated.durationMs,
50+ RATE_LIMITS.sse.unauthenticated.durationMs
51+);
52+
44 // Lazy cleanup: remove stale entries on each check (Cloudflare Workers don't allow setInterval at global scope)53 // Lazy cleanup: remove stale entries on each check (Cloudflare Workers don't allow setInterval at global scope)
45 let lastCleanup = 0;54 let lastCleanup = 0;
46-const CLEANUP_INTERVAL = 5 * 60 * 1000; // 5 minutes
47 55
48 function cleanupStaleEntries() {56 function cleanupStaleEntries() {
49 const now = Date.now();57 const now = Date.now();
50- if (now - lastCleanup < CLEANUP_INTERVAL) return;58+ // Run cleanup at most once per max duration window
59+ if (now - lastCleanup < MAX_RATE_LIMIT_DURATION) return;
51 lastCleanup = now;60 lastCleanup = now;
52 61
53 for (const [ip, timestamps] of ipRequests.entries()) {62 for (const [ip, timestamps] of ipRequests.entries()) {
54- const filtered = timestamps.filter((t) => now - t.timestamp < CLEANUP_INTERVAL);63+ const filtered = timestamps.filter((t) => now - t.timestamp < MAX_RATE_LIMIT_DURATION);
55 if (filtered.length === 0) {64 if (filtered.length === 0) {
56 ipRequests.delete(ip);65 ipRequests.delete(ip);
57 } else {66 } else {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 8

↑↓ moveenter openesc close