fix(realtime): carry SSE Retry-After into the reconnect outcome (04-02)
A 429/503 Retry-After reached only onRetryHint, so passing the read result to the reconnect coordinator retried before the server allowed. readSseForReconnect returns the hint as serverNotBeforeMs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
bcad7b0d12
commit
02d3fdf546
@@ -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<RealtimeReconnectOutcome<FetchSseClosedOutcome>> {
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export {
|
||||
createFetchSseConnection,
|
||||
readSseForReconnect,
|
||||
SSE_CONTINUE,
|
||||
type FetchSseClosedOutcome,
|
||||
type FetchSseConnection,
|
||||
|
||||
@@ -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<typeof fetch>()
|
||||
.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<never>;
|
||||
},
|
||||
});
|
||||
|
||||
// 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]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user