fix(memories): wait for a public media copy just under the proxy timeout

No public page retries a 503 by itself, so a first view of a large video that took over 10 s showed a dead player. The request now waits up to 50 s before answering come back later.
This commit is contained in:
Maurice
2026-11-23 19:57:47 +01:00
parent e6a5ba095d
commit b3845dbdfc
2 changed files with 36 additions and 6 deletions
@@ -25,11 +25,15 @@ const RENDITION_CATEGORIES: readonly ServedCategory[] = ['avatars', 'covers', 'j
const REENCODABLE_EXTENSIONS = new Set(['.bmp', '.gif', '.jpeg', '.jpg', '.png', '.tif', '.tiff']);
/**
* How long a request waits for a copy being made. A large video takes longer
* than a reverse proxy waits for the first byte, so past this the request is
* told to come back while the copy goes on being made.
* How long a request waits for a copy being made: just under the 60 s a
* reverse proxy commonly waits for the first byte, since no public page
* retries a 503 by itself. A video that takes longer still is answered with
* come back later while the copy goes on being made.
*/
const RENDITION_WAIT_MS = 10_000;
const RENDITION_WAIT_MS = 50_000;
/** The Retry-After sent with a pending answer, in seconds. */
const RENDITION_RETRY_AFTER_S = 10;
/** The copy of a name is still being made; the request is answered with {@link answerRenditionPending}. */
export class RenditionPendingError extends Error {
@@ -48,7 +52,7 @@ export function pendingOrNone(err: unknown): typeof RENDITION_PENDING | null {
/** Answers a request whose copy is still being made: 503, come back in a while, nothing cached. */
export function answerRenditionPending(res: Response): void {
res.set('Retry-After', String(RENDITION_WAIT_MS / 1000));
res.set('Retry-After', String(RENDITION_RETRY_AFTER_S));
res.set('Cache-Control', 'no-store');
res.status(503).json({ error: 'The file is being prepared, try again shortly' });
}
@@ -126,7 +126,7 @@ describe('renditionFor', () => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
try {
const first = expect(service.renditionFor('journey', 'long.jpg')).rejects.toBeInstanceOf(RenditionPendingError);
await vi.advanceTimersByTimeAsync(10_000);
await vi.advanceTimersByTimeAsync(50_000);
await first;
} finally {
vi.useRealTimers();
@@ -139,6 +139,32 @@ describe('renditionFor', () => {
expect(await exifr.gps(fs.readFileSync(at('journey', 'public/long.jpg')))).toBeUndefined();
});
it('PMEDIA-006b: a copy that takes half a minute is still answered by the request that waits for it', async () => {
write('journey', 'slow.jpg', phonePhoto());
let release = () => {};
let started = () => {};
const held = new Promise<void>((resolve) => (release = resolve));
const building = new Promise<void>((resolve) => (started = resolve));
storage.withLocalFile.mockImplementationOnce(async (category, name, fn) => {
started();
await held;
return fn(at(category, name));
});
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
try {
const settled = service.renditionFor('journey', 'slow.jpg').then(
(name) => ({ name }),
(err: unknown) => ({ err }),
);
await building;
await vi.advanceTimersByTimeAsync(30_000);
release();
expect(await settled).toEqual({ name: 'public/slow.jpg' });
} finally {
vi.useRealTimers();
}
});
it('PMEDIA-003: refuses what it can neither clean nor re-encode, a copy by its own name, and a source that is gone', async () => {
write('photos', 'c.bmp', Buffer.from(`BM${'x'.repeat(40)}`));
expect(await service.renditionFor('photos', 'c.bmp')).toBeNull();