Load limiting safeguards

This commit is contained in:
2026-05-06 04:04:53 +02:00
parent 47f6971c56
commit 2484ed2815
22 changed files with 374 additions and 144 deletions

View File

@@ -1,2 +1,27 @@
export const API = "http://127.0.0.1:8000";
export const POLL = 15_000;
export const POLL_HEAVY = 60_000;
export const POLL_HIDDEN = 120_000;
export const FETCH_TIMEOUT = 12_000;
export function pollDelay({ok = true, base = POLL, failures = 0} = {}) {
const hidden = typeof document !== "undefined" && document.visibilityState === "hidden";
const failureDelay = ok ? base : Math.min(base * 2 ** Math.min(failures, 4), 300_000);
return hidden ? Math.max(failureDelay, POLL_HIDDEN) : failureDelay;
}
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export async function apiFetch(path, options = {}) {
const url = path.startsWith("http") ? path : `${API}${path}`;
const timeout = options.timeout ?? FETCH_TIMEOUT;
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), timeout);
try {
return await fetch(url, {...options, signal: ctrl.signal});
} finally {
clearTimeout(timer);
}
}