diff --git a/server/src/nest/memories/public-media.service.ts b/server/src/nest/memories/public-media.service.ts index 95668c912..9b4456f5b 100644 --- a/server/src/nest/memories/public-media.service.ts +++ b/server/src/nest/memories/public-media.service.ts @@ -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' }); } diff --git a/server/tests/unit/nest/memories/public-media.service.test.ts b/server/tests/unit/nest/memories/public-media.service.test.ts index 92938a7f5..e552b48f1 100644 --- a/server/tests/unit/nest/memories/public-media.service.test.ts +++ b/server/tests/unit/nest/memories/public-media.service.test.ts @@ -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((resolve) => (release = resolve)); + const building = new Promise((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();