diff --git a/src/adapters/realtime/sse/fetch-sse-connection.ts b/src/adapters/realtime/sse/fetch-sse-connection.ts index 049a186b..f197c047 100644 --- a/src/adapters/realtime/sse/fetch-sse-connection.ts +++ b/src/adapters/realtime/sse/fetch-sse-connection.ts @@ -12,6 +12,7 @@ import { } from "../../../application/ports/realtime/event-authority.ts"; import { isRealtimeResumeCursor } from "../../../contracts/realtime-events.ts"; import { systemClock } from "../../platform/system-clock.ts"; +import type { RealtimeReconnectOutcome } from "../reconnect-coordinator.ts"; import { parseRetryAfterDelay } from "../reconnect-policy.ts"; import { isRealtimeResult, @@ -468,6 +469,28 @@ export function createFetchSseConnection( return Object.freeze({ read, close }); } +/** + * Bridges one read to the reconnect coordinator's outcome shape. A validated + * Retry-After (429/503) or SSE `retry:` hint reaches only `onRetryHint`, not + * the failure, so a plain `read` result lets the coordinator retry early. + */ +export async function readSseForReconnect( + connection: FetchSseConnection, + input: FetchSseReadInput, +): Promise> { + let serverNotBeforeMs: number | null = null; + const result = await connection.read({ + ...input, + onRetryHint(retryMs) { + serverNotBeforeMs = retryMs; + input.onRetryHint?.(retryMs); + }, + }); + return Object.freeze( + result.ok ? { result } : { result, serverNotBeforeMs }, + ); +} + function fixedEndpoint(endpoint: string, applicationOrigin: string): URL { let parsedEndpoint: URL; let parsedOrigin: URL; diff --git a/src/adapters/realtime/sse/index.ts b/src/adapters/realtime/sse/index.ts index 49cc802c..7608a3b8 100644 --- a/src/adapters/realtime/sse/index.ts +++ b/src/adapters/realtime/sse/index.ts @@ -1,5 +1,6 @@ export { createFetchSseConnection, + readSseForReconnect, SSE_CONTINUE, type FetchSseClosedOutcome, type FetchSseConnection, diff --git a/tests/unit/realtime/realtime-reconnect-coordinator.test.ts b/tests/unit/realtime/realtime-reconnect-coordinator.test.ts index 268304c8..5d9a06d4 100644 --- a/tests/unit/realtime/realtime-reconnect-coordinator.test.ts +++ b/tests/unit/realtime/realtime-reconnect-coordinator.test.ts @@ -30,6 +30,7 @@ import { } from "../../../src/adapters/realtime/result.ts"; import { createFetchSseConnection, + readSseForReconnect, SSE_CONTINUE, } from "../../../src/adapters/realtime/sse/fetch-sse-connection.ts"; import { @@ -1879,4 +1880,69 @@ describe("transport failures through the reconnect coordinator", () => { expect(fetcher).toHaveBeenCalledTimes(2); }, ); + + it.each([429, 503])( + "waits at least the SSE %i Retry-After through the reconnect bridge", + async (status) => { + const clock = new ManualClock(); + const fetcher = vi + .fn() + .mockResolvedValueOnce( + new Response(null, { + status, + headers: { "retry-after": "3" }, + }), + ) + .mockResolvedValueOnce( + new Response(null, { + status, + headers: { "retry-after": "10" }, + }), + ); + const coordinator = createRealtimeReconnectCoordinator({ + policy: policy({ maxDelayMs: 5_000, maxElapsedMs: 60_000 }), + environment: new MutableOnlineEnvironment(true), + clock, + random: () => 0.5, + confirmTransportBarrier: confirmationSucceeded, + attempt: async (context) => { + const read = await readSseForReconnect( + createFetchSseConnection({ + endpoint: "https://app.example.test/events", + applicationOrigin: "https://app.example.test", + recoveryMode: "CURSOR", + fetcher, + clock, + }), + { + resumeCursor: null, + signal: context.signal, + onEvent: () => realtimeSuccess(SSE_CONTINUE), + }, + ); + // Only connect failures are exercised here. + if (read.result.ok) { + return failed( + realtimeFailure("PROTOCOL_MISMATCH", "CONNECT", false), + ); + } + return read as RealtimeReconnectOutcome; + }, + }); + + // 10_000 is each read's connect timeout; 3_000 is the reconnect wait. + const running = coordinator.run(); + await flushUntil(() => clock.sleepCalls.length === 2); + expect(clock.sleepCalls).toEqual([10_000, 3_000]); + clock.advance(3_000); + + // A hint above the policy's maxDelayMs still ends the run. + await expect(running).resolves.toMatchObject({ + ok: false, + error: { operation: "CONNECT", retryable: true }, + }); + expect(fetcher).toHaveBeenCalledTimes(2); + expect(clock.sleepCalls).toEqual([10_000, 3_000, 10_000]); + }, + ); });