fix(realtime): reset the retry budget on every session end and pause it offline (04-01)
A long healthy connection that ended offline or with a throwing waitClosed kept a spent elapsed budget and stopped with zero retries. Offline waits no longer spend the budget, and WebSocket HEARTBEAT_ACK is now observable so a quiet stream can earn a fresh budget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4d17259037
commit
bcad7b0d12
@@ -55,7 +55,9 @@ export type RealtimeReconnectAttemptContext = Readonly<{
|
||||
pendingRecovery: RealtimeCommittedRecovery | null;
|
||||
isCurrent(): boolean;
|
||||
/**
|
||||
* Call only after a protocol-valid heartbeat or event.
|
||||
* Call only after a protocol-valid heartbeat or event: an applied event,
|
||||
* an SSE `onComment`, or a WebSocket `HEARTBEAT_ACK` observation. A quiet
|
||||
* stream that never calls it cannot earn a fresh retry budget.
|
||||
*/
|
||||
markValidHeartbeatOrEvent(): void;
|
||||
/**
|
||||
@@ -493,15 +495,16 @@ export function createRealtimeReconnectCoordinator<ClosedReceipt>(
|
||||
) {
|
||||
return retry.terminalResult;
|
||||
}
|
||||
if (
|
||||
(!online || requiredOnlineVersion !== null) &&
|
||||
!(await waitOnline())
|
||||
) {
|
||||
const offlineWait = !online || requiredOnlineVersion !== null;
|
||||
if (offlineWait && !(await waitOnline())) {
|
||||
return failure(currentFailure() ?? "ABORTED");
|
||||
}
|
||||
|
||||
now = safeNow(clock);
|
||||
if (now === null) return retry.terminalResult;
|
||||
// Offline time is not retry time. The elapsed budget restarts when
|
||||
// the network returns; maxAttempts still bounds the loop.
|
||||
if (offlineWait) budgetStartedAt = now;
|
||||
const delay = nextDelay(
|
||||
retry,
|
||||
retriesUsed,
|
||||
@@ -815,6 +818,21 @@ export function createRealtimeReconnectCoordinator<ClosedReceipt>(
|
||||
phase.controller.signal,
|
||||
);
|
||||
const closeOffline = wentOffline(phase);
|
||||
// Every way an opened session ends, including offline and a throwing
|
||||
// waitClosed, can earn a fresh budget, not only a classified close.
|
||||
const endedAt = safeNow(clock);
|
||||
if (
|
||||
endedAt !== null &&
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: openedAt,
|
||||
nowMs: endedAt,
|
||||
observedValidHeartbeatOrEvent: validSignalObserved,
|
||||
})
|
||||
) {
|
||||
retriesUsed = 0;
|
||||
budgetStartedAt = endedAt;
|
||||
}
|
||||
|
||||
const afterWaitClosed = currentFailure();
|
||||
if (afterWaitClosed) {
|
||||
@@ -902,18 +920,6 @@ export function createRealtimeReconnectCoordinator<ClosedReceipt>(
|
||||
if (closedAt === null) {
|
||||
return classified.outcome.failure.result;
|
||||
}
|
||||
if (
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: openedAt,
|
||||
nowMs: closedAt,
|
||||
observedValidHeartbeatOrEvent:
|
||||
validSignalObserved,
|
||||
})
|
||||
) {
|
||||
retriesUsed = 0;
|
||||
budgetStartedAt = closedAt;
|
||||
}
|
||||
retry = captureRetry(
|
||||
classified.outcome.failure.result,
|
||||
classified.outcome.failure.result,
|
||||
@@ -932,17 +938,6 @@ export function createRealtimeReconnectCoordinator<ClosedReceipt>(
|
||||
if (closedAt === null) {
|
||||
return classified.directive.terminalFailure.result;
|
||||
}
|
||||
if (
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: openedAt,
|
||||
nowMs: closedAt,
|
||||
observedValidHeartbeatOrEvent: validSignalObserved,
|
||||
})
|
||||
) {
|
||||
retriesUsed = 0;
|
||||
budgetStartedAt = closedAt;
|
||||
}
|
||||
pendingRecovery = classified.directive;
|
||||
retry = captureRetry(
|
||||
classified.directive.terminalFailure.result,
|
||||
|
||||
@@ -71,6 +71,7 @@ export type WebSocketConnectionObservation = Readonly<{
|
||||
kind:
|
||||
| "CONNECTED"
|
||||
| "SUBSCRIBED"
|
||||
| "HEARTBEAT_ACK"
|
||||
| "FRAME_REJECTED"
|
||||
| "RECOVERY_REQUIRED"
|
||||
| "CLOSED";
|
||||
@@ -372,6 +373,9 @@ export function createWebSocketConnection(
|
||||
if (decoded.value.nonce === retiredHeartbeat) {
|
||||
retiredHeartbeat = null;
|
||||
}
|
||||
// A quiet stream's only liveness proof; composition can forward it to
|
||||
// the reconnect coordinator's markValidHeartbeatOrEvent.
|
||||
observe({ kind: "HEARTBEAT_ACK" });
|
||||
return;
|
||||
}
|
||||
const queueLimits = effectiveLimits ?? ceilings;
|
||||
|
||||
Reference in New Issue
Block a user