Rate limits per user, and coalesce live-event reloads (#10)
CI / Lint (eslint) (pull_request) Successful in 23s
CI / Test (jest) (pull_request) Successful in 23s
CI / Security (npm audit) (pull_request) Successful in 1m17s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m42s
CI / Integration (api) (pull_request) Successful in 4m23s
CI / UI (playwright) (pull_request) Successful in 2m35s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m13s
CI / Lint (eslint) (pull_request) Successful in 23s
CI / Test (jest) (pull_request) Successful in 23s
CI / Security (npm audit) (pull_request) Successful in 1m17s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m42s
CI / Integration (api) (pull_request) Successful in 4m23s
CI / UI (playwright) (pull_request) Successful in 2m35s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m13s
Production refused 1,052 of the UI's requests with 429 in one day: every user arrives through the reverse proxy, so the per-IP limiter put the whole team in one bucket, and each open page reloaded its whole list on every live event (about two per Gandalf command). - lib/ratelimit.js keys both limiters: a trusted SSO identity gets user:<name>, a request with the valid Gandalf key gets 'gandalf' (an invalid key falls back to the IP, so nobody can drain Gandalf's bucket), everything else the IP. The API default goes from 300 to 1000 per 15 min per bucket. - Pulse.util.throttle: live-event reloads on Executions, Dashboard and Workers run at most once per 2 s (at once when idle, then once at the end of the window). User actions still reload immediately. - Unit tests for the keys. No visual change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a01c28e06
commit
6ffe230c34
7 files changed
+98
-9
No files matched your search
+20
-1
@@ -633,6 +633,25 @@
|
||||
} catch (e) { err('autoRefresh start failed', e); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Run fn at most once per `ms`: at once when idle, otherwise once at the end of the
|
||||
* window (so the last event is never lost). Live events can arrive several times a
|
||||
* second; reloading on each one used up the API rate limit (#10).
|
||||
*/
|
||||
function throttle(fn, ms) {
|
||||
let last = 0;
|
||||
let timer = null;
|
||||
return function throttled() {
|
||||
const wait = last + ms - Date.now();
|
||||
if (wait <= 0 && !timer) {
|
||||
last = Date.now();
|
||||
fn();
|
||||
} else if (!timer) {
|
||||
timer = setTimeout(() => { timer = null; last = Date.now(); fn(); }, Math.max(wait, 0));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
9. Public surface
|
||||
------------------------------------------------------------------- */
|
||||
@@ -654,7 +673,7 @@
|
||||
get beep() { return LT().beep; },
|
||||
esc: esc,
|
||||
fmt: fmt,
|
||||
util: { download: download, storage: storage },
|
||||
util: { download: download, storage: storage, throttle: throttle },
|
||||
|
||||
/** Escape hatch for pages that need to push a frame upstream. */
|
||||
ws: { send(d) { return _wsHandle && _wsHandle.send ? _wsHandle.send(d) : false; }, get handle() { return _wsHandle; } },
|
||||
|
||||
@@ -205,15 +205,23 @@
|
||||
return refresh();
|
||||
}
|
||||
|
||||
// Live events come in bursts; reload at most once per 2 s each (#10).
|
||||
const reloadWorkers = Pulse.util.throttle(() => {
|
||||
Promise.all([loadWorkers(), loadRunningCount()]).then(([workers, rt]) => renderStats(workers, rt));
|
||||
}, 2000);
|
||||
const reloadExecutions = Pulse.util.throttle(() => {
|
||||
Promise.all([loadExecutions(), loadRunningCount()]).then(([, rt]) => renderStats(_workers, rt));
|
||||
}, 2000);
|
||||
|
||||
function onEvent(type) {
|
||||
if (type === 'worker_update') {
|
||||
Promise.all([loadWorkers(), loadRunningCount()]).then(([workers, rt]) => renderStats(workers, rt));
|
||||
reloadWorkers();
|
||||
return true;
|
||||
}
|
||||
if (type === 'execution_started' || type === 'execution_status' ||
|
||||
type === 'command_result' || type === 'workflow_result' ||
|
||||
type === 'executions_bulk_deleted') {
|
||||
Promise.all([loadExecutions(), loadRunningCount()]).then(([, rt]) => renderStats(_workers, rt));
|
||||
reloadExecutions();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -100,10 +100,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadCurrent() {
|
||||
async function reloadNow() {
|
||||
await loadView(state.view, false);
|
||||
render();
|
||||
}
|
||||
// Live events trigger this; coalesce bursts into at most one reload per 2 s (#10).
|
||||
const reloadCurrent = Pulse.util.throttle(reloadNow, 2000);
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
Rendering: list
|
||||
@@ -694,7 +696,7 @@
|
||||
toast('error', (e && e.message) || 'Failed to delete executions');
|
||||
return;
|
||||
}
|
||||
await reloadCurrent();
|
||||
await reloadNow();
|
||||
},
|
||||
|
||||
'ex:abort': async (el) => {
|
||||
@@ -716,7 +718,7 @@
|
||||
toast('error', (e && e.message) || 'Failed to abort execution');
|
||||
return;
|
||||
}
|
||||
await reloadCurrent();
|
||||
await reloadNow();
|
||||
},
|
||||
|
||||
/* Frozen cross-page contract: quick.js consumes and clears pulse_rerun. */
|
||||
@@ -771,7 +773,7 @@
|
||||
return;
|
||||
}
|
||||
await openDetail(id, true);
|
||||
await reloadCurrent();
|
||||
await reloadNow();
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -258,9 +258,12 @@
|
||||
return loadWorkers();
|
||||
}
|
||||
|
||||
// Heartbeats from many workers arrive in bursts; reload at most once per 2 s (#10).
|
||||
const reloadWorkers = Pulse.util.throttle(() => { loadWorkers(); }, 2000);
|
||||
|
||||
function onEvent(type) {
|
||||
if (type === 'worker_update') {
|
||||
loadWorkers();
|
||||
reloadWorkers();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user