diff --git a/src/shared/remote-runtime/control/remote-runtime-shared-control-reconnect.ts b/src/shared/remote-runtime/control/remote-runtime-shared-control-reconnect.ts index 18cc05d08c2..f998c492a40 100644 --- a/src/shared/remote-runtime/control/remote-runtime-shared-control-reconnect.ts +++ b/src/shared/remote-runtime/control/remote-runtime-shared-control-reconnect.ts @@ -1,4 +1,4 @@ -import { scheduleSharedControlReconnect } from './remote-runtime-shared-control-state' +import { withReconnectJitter } from '../../reconnect-jitter' const ACTIVE_RECONNECT_DELAYS_MS = [250, 500, 1000, 2000, 4000, 8000, 15_000, 30_000] const IDLE_RECONNECT_DELAYS_MS = [...ACTIVE_RECONNECT_DELAYS_MS, 60_000, 120_000, 300_000] @@ -100,3 +100,23 @@ export class SharedControlReconnectScheduler { this.attempt = 0 } } + +function scheduleSharedControlReconnect(args: { + current: ReturnType | null + intentionallyClosed: boolean + reconnectAttempt: number + delaysMs: readonly number[] + open: () => void +}): { timer: ReturnType | null; reconnectAttempt: number } { + if (args.current || args.intentionallyClosed) { + return { timer: args.current, reconnectAttempt: args.reconnectAttempt } + } + const delay = withReconnectJitter( + args.delaysMs[Math.min(args.reconnectAttempt, args.delaysMs.length - 1)] + ) + const timer = setTimeout(args.open, delay) + if (typeof timer.unref === 'function') { + timer.unref() + } + return { timer, reconnectAttempt: args.reconnectAttempt + 1 } +} diff --git a/src/shared/remote-runtime/control/remote-runtime-shared-control-state.ts b/src/shared/remote-runtime/control/remote-runtime-shared-control-state.ts index 533278f3af7..f1cc6ecd1e5 100644 --- a/src/shared/remote-runtime/control/remote-runtime-shared-control-state.ts +++ b/src/shared/remote-runtime/control/remote-runtime-shared-control-state.ts @@ -8,7 +8,6 @@ import type { SharedControlReadyWaiter } from './remote-runtime-shared-control-types' import { getSubscriptionId, isEndResult } from './remote-runtime-shared-control-protocol' -import { withReconnectJitter } from '../../reconnect-jitter' import { tagRuntimeSubscriptionReplayResponse } from '../../runtime-subscription-replay' export function rejectSharedControlPendingRequest( @@ -162,23 +161,3 @@ export function closeSharedControlSocketState(args: { // Best-effort cleanup of remote runtime control socket. } } - -export function scheduleSharedControlReconnect(args: { - current: ReturnType | null - intentionallyClosed: boolean - reconnectAttempt: number - delaysMs: readonly number[] - open: () => void -}): { timer: ReturnType | null; reconnectAttempt: number } { - if (args.current || args.intentionallyClosed) { - return { timer: args.current, reconnectAttempt: args.reconnectAttempt } - } - const delay = withReconnectJitter( - args.delaysMs[Math.min(args.reconnectAttempt, args.delaysMs.length - 1)] - ) - const timer = setTimeout(args.open, delay) - if (typeof timer.unref === 'function') { - timer.unref() - } - return { timer, reconnectAttempt: args.reconnectAttempt + 1 } -}