Open source CapCut alternative.
TypeScript
fix(site): gate cloud requests after a refusal [rebuild]
David Le479ffe2a376f
diff --git a/site/src/cut/lib/backend/cloud.ts b/site/src/cut/lib/backend/cloud.tsindex 6060163d..3adc41b3 100644--- a/site/src/cut/lib/backend/cloud.ts+++ b/site/src/cut/lib/backend/cloud.ts@@ -31,19 +31,49 @@ async function requestWithSession(bound: CloudSession, path: string, init?: Requ// (site/firewall/), so the same request goes again once the window passes.// A route that throttles answers JSON with its reason, and that stands.for (let attempt = 1; ; attempt++) {+ await firewallGate();const res = await fetch((bound?.base ?? "") + path, { ...init, headers });const wait = throttleWait(res);- if (wait === null || attempt > FIREWALL_RETRIES || !firewallRefusal(res) || !resendable(init?.body) || init?.signal?.aborted) {+ if (wait === null || !firewallRefusal(res)) {+ return observeOperationResponse(res);+ }+ holdFirewall(wait);+ if (attempt > FIREWALL_RETRIES || !resendable(init?.body) || init?.signal?.aborted) {return observeOperationResponse(res);}await res.body?.cancel().catch(() => {});- await new Promise((r) => setTimeout(r, wait));}}// How many times a request the firewall refused goes again.const FIREWALL_RETRIES = 2;+// The firewall counts one address, so one refusal holds every cloud request+// this process sends until its window passes. Each then goes at a random+// point in a spread after it, half the window wide, so the burst that tripped+// the window does not land in the next one all at once: a 10 s window+// reopens over 10–15 s.+const FIREWALL_SPREAD = 0.5;+let firewallOpensAt = 0;+let firewallWindowMs = 0;++function holdFirewall(waitMs: number) {+ const opensAt = Date.now() + waitMs;+ if (opensAt <= firewallOpensAt) {+ return;+ }+ firewallOpensAt = opensAt;+ firewallWindowMs = waitMs;+}++async function firewallGate() {+ const wait = firewallOpensAt - Date.now();+ if (wait <= 0) {+ return;+ }+ await new Promise((r) => setTimeout(r, wait + Math.random() * firewallWindowMs * FIREWALL_SPREAD));+}+/** Whether a throttle came from the firewall: its refusal is plain text. */const firewallRefusal = (res: Response) => !/json/i.test(res.headers.get("content-type") ?? "");diff --git a/site/src/cut/lib/backend/cloudCapture.test.ts b/site/src/cut/lib/backend/cloudCapture.test.tsindex 4cd6e123..fc710522 100644--- a/site/src/cut/lib/backend/cloudCapture.test.ts+++ b/site/src/cut/lib/backend/cloudCapture.test.ts@@ -62,3 +62,24 @@ test("a throttle a route answers itself stands", async () => {expect(fetcher.mock.calls.length).toBe(1);} finally { fetcher.mockRestore(); }});++test("a firewall refusal holds every request until its window passes", async () => {+ const WINDOW_MS = 100;+ const sentAt: number[] = [];+ const fetcher = spyOn(globalThis, "fetch").mockImplementation((async () => {+ sentAt.push(Date.now());+ return sentAt.length === 1+ ? new Response("Too Many Requests", { status: 429, headers: { "Retry-After": String(WINDOW_MS / 1000), "Content-Type": "text/plain" } })+ : Response.json({ ok: true });+ }) as typeof fetch);+ try {+ // The second request starts after the first was refused: it waits the+ // window out with it, so the burst does not refill the next window.+ const first = cloudBackend.fetch("/api/cut/projects", { method: "GET" });+ await new Promise((r) => setTimeout(r, 10));+ const second = cloudBackend.fetch("/api/cut/projects/p1", { method: "GET" });+ await Promise.all([first, second]);+ expect(sentAt.length).toBe(3);+ expect(Math.min(...sentAt.slice(1)) - sentAt[0]).toBeGreaterThanOrEqual(WINDOW_MS - 5);+ } finally { fetcher.mockRestore(); }+});