parent
926c640c6f
commit
cc56795ca3
@ -0,0 +1,35 @@
|
||||
import {
|
||||
CONNECTION_DIAGNOSTIC_EVENTS,
|
||||
CONNECTION_STATE_FAILED,
|
||||
LOG_MSG_RECONNECT_EXHAUSTED,
|
||||
TIMER_KEY_RECONNECT
|
||||
} from './consts.ts';
|
||||
import { emitConnectionDiagnostic, type ConnectionDiagnostics } from './diagnostics.ts';
|
||||
import type { ConnectionReconnectPolicy } from './reconnect.ts';
|
||||
import type { ConnectionStateTracker } from './connection-state.ts';
|
||||
import type { ConnectionTimerControls } from './connection-timers.ts';
|
||||
|
||||
export interface ScheduleConnectionReconnectInput {
|
||||
readonly policy: ConnectionReconnectPolicy;
|
||||
readonly state: ConnectionStateTracker;
|
||||
readonly timers: ConnectionTimerControls;
|
||||
readonly diagnostics: ConnectionDiagnostics;
|
||||
readonly connect: () => void;
|
||||
}
|
||||
|
||||
export function scheduleConnectionReconnect(input: ScheduleConnectionReconnectInput): void {
|
||||
const nextReconnect = input.policy.next(input.state.reconnectAttempt);
|
||||
if (!nextReconnect.ok) {
|
||||
emitConnectionDiagnostic(input.diagnostics, CONNECTION_DIAGNOSTIC_EVENTS.RECONNECT_EXHAUSTED, {
|
||||
reconnectAttempt: nextReconnect.reconnectAttempt,
|
||||
maxAttempts: nextReconnect.maxAttempts
|
||||
});
|
||||
const exhaustedError = new Error(LOG_MSG_RECONNECT_EXHAUSTED);
|
||||
input.state.setError(exhaustedError);
|
||||
input.state.transition(CONNECTION_STATE_FAILED, exhaustedError);
|
||||
return;
|
||||
}
|
||||
|
||||
input.state.setReconnectAttempt(nextReconnect.attempt);
|
||||
input.timers.schedule(TIMER_KEY_RECONNECT, nextReconnect.delayMs, input.connect);
|
||||
}
|
||||
Loading…
Reference in new issue