Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): pace archived media previews under the gateway throttle
Yudhi Armyndharisc04270f61754
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts b/dashboard/src/components/chats/ArchivedMediaPreview.test.tsindex 7b8fd7c63..ff543f47a 100644--- a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts+++ b/dashboard/src/components/chats/ArchivedMediaPreview.test.ts@@ -237,3 +237,28 @@ test('an archived audio player reports its arrival so a bottom-pinned thread staawait rtl.waitFor(() => assert.ok(container.querySelector('audio.chat-audio-media'), 'audio did not render'));assert.equal(reports, 1, 'the thread is told once that the placeholder became a player');});++test('previews mounted together start their fetches apart, so a burst stays under the gateway throttle', async () => {+ const starts: number[] = [];+ const preview = (key: number) =>+ createElement(ArchivedMediaPreview, {+ key,+ kind: 'audio',+ fallback,+ load: async () => {+ starts.push(Date.now());+ return new Blob(['x']);+ },+ });+ const { container } = rtl.render(createElement('div', null, [0, 1, 2, 3].map(preview)));+ await rtl.waitFor(() => assert.equal(container.querySelectorAll('audio.chat-audio-media').length, 4), {+ timeout: 3000,+ });+ assert.equal(starts.length, 4);+ for (let i = 1; i < starts.length; i++) {+ assert.ok(+ starts[i] - starts[i - 1] >= 100,+ `fetches ${i - 1} and ${i} started ${starts[i] - starts[i - 1]} ms apart`,+ );+ }+});diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.tsx b/dashboard/src/components/chats/ArchivedMediaPreview.tsxindex 9b999c86f..c5b5c3880 100644--- a/dashboard/src/components/chats/ArchivedMediaPreview.tsx+++ b/dashboard/src/components/chats/ArchivedMediaPreview.tsx@@ -1,6 +1,7 @@import { useEffect, useRef, useState, type ReactNode, type RefObject } from 'react';import { Loader2 } from 'lucide-react';import MediaLightbox from './MediaLightbox';+import { loadArchivedMedia } from '../../utils/archivedMediaLoad';export type ArchivedMediaKind = 'image' | 'video' | 'audio';@@ -100,8 +101,7 @@ function ArchivedMediaPreview({if (state.phase !== 'loading') return undefined;let cancelled = false;const controller = new AbortController();- loadRef- .current(controller.signal)+ loadArchivedMedia(controller.signal, () => loadRef.current(controller.signal)).then(blob => {if (cancelled) return;const url = URL.createObjectURL(blob);diff --git a/dashboard/src/utils/archivedMediaLoad.test.ts b/dashboard/src/utils/archivedMediaLoad.test.tsnew file mode 100644index 000000000..2629cb091--- /dev/null+++ b/dashboard/src/utils/archivedMediaLoad.test.ts@@ -0,0 +1,75 @@+import { test, before } from 'node:test';+import assert from 'node:assert/strict';+import { loadArchivedMedia } from './archivedMediaLoad.ts';++before(async () => {+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');+ await installJsdomGlobals();+});++const status = (code: number) => Object.assign(new Error(`HTTP ${code}`), { status: code });++test('a burst of loads starts at least 100 ms apart, under the gateway per-second throttle', async () => {+ const starts: number[] = [];+ const { signal } = new AbortController();+ await Promise.all(+ Array.from({ length: 4 }, () =>+ loadArchivedMedia(signal, async () => {+ starts.push(Date.now());+ return 'x';+ }),+ ),+ );+ assert.equal(starts.length, 4);+ for (let i = 1; i < starts.length; i++) {+ assert.ok(+ starts[i] - starts[i - 1] >= 100,+ `loads ${i - 1} and ${i} started ${starts[i] - starts[i - 1]} ms apart`,+ );+ }+});++test('a load aborted while it waits for its turn never starts', async () => {+ // Three loads ahead of it keep the next turn a few hundred milliseconds away.+ const ahead = Array.from({ length: 3 }, () => loadArchivedMedia(new AbortController().signal, async () => 'x'));+ const controller = new AbortController();+ let ran = false;+ const waiting = loadArchivedMedia(controller.signal, async () => {+ ran = true;+ return 'x';+ });+ controller.abort();+ await assert.rejects(waiting);+ await Promise.all(ahead);+ assert.equal(ran, false);+});++test('a throttled load is retried and then resolves', async () => {+ let calls = 0;+ const value = await loadArchivedMedia(+ new AbortController().signal,+ async () => {+ calls += 1;+ if (calls === 1) throw status(429);+ return 'bytes';+ },+ 5,+ );+ assert.equal(value, 'bytes');+ assert.equal(calls, 2);+});++test('any other failure is not retried', async () => {+ let calls = 0;+ await assert.rejects(+ loadArchivedMedia(+ new AbortController().signal,+ async () => {+ calls += 1;+ throw status(404);+ },+ 5,+ ),+ );+ assert.equal(calls, 1);+});diff --git a/dashboard/src/utils/archivedMediaLoad.ts b/dashboard/src/utils/archivedMediaLoad.tsnew file mode 100644index 000000000..87aeae994--- /dev/null+++ b/dashboard/src/utils/archivedMediaLoad.ts@@ -0,0 +1,35 @@+import { retryThrottled } from './fetchAllPages.ts';++// The gateway answers a route 10 times a second per IP by default and refuses the rest with a 429,+// which also counts against the minute tier. A chat opened on archived media wants one fetch per+// preview near the viewport, so the fetches start 125 ms apart (8 a second) rather than all at once;+// otherwise the newest previews are the ones refused and stay download buttons for good. A 429 that+// still arrives (a lower configured limit, another tab) is retried like fetchAllPages retries a page.+const START_GAP_MS = 125;+let nextStart = 0;++function waitForTurn(signal: AbortSignal): Promise<void> {+ if (signal.aborted) return Promise.reject(signal.reason);+ const now = Date.now();+ const startAt = Math.max(now, nextStart);+ nextStart = startAt + START_GAP_MS;+ if (startAt === now) return Promise.resolve();+ return new Promise((resolve, reject) => {+ const onAbort = () => {+ clearTimeout(timer);+ reject(signal.reason);+ };+ const timer = setTimeout(() => {+ signal.removeEventListener('abort', onAbort);+ resolve();+ }, startAt - now);+ signal.addEventListener('abort', onAbort, { once: true });+ });+}++export function loadArchivedMedia<T>(signal: AbortSignal, load: () => Promise<T>, retryDelayMs = 1000): Promise<T> {+ return retryThrottled(async () => {+ await waitForTurn(signal);+ return load();+ }, retryDelayMs);+}diff --git a/dashboard/src/utils/fetchAllPages.ts b/dashboard/src/utils/fetchAllPages.tsindex 7d9d888a3..f837327b7 100644--- a/dashboard/src/utils/fetchAllPages.ts+++ b/dashboard/src/utils/fetchAllPages.ts@@ -19,7 +19,7 @@ interface FetchAllPagesOptions {// route, and an hour's budget holds about 20 such walks.const THROTTLE_RETRIES = 2;-async function retryThrottled<T>(fetchOnce: () => Promise<T>, delayMs: number): Promise<T> {+export async function retryThrottled<T>(fetchOnce: () => Promise<T>, delayMs: number): Promise<T> {for (let attempt = 0; ; attempt++) {try {return await fetchOnce();