Extract connection reconnect scheduler

master
dev 5 months ago
parent 926c640c6f
commit cc56795ca3

@ -19,6 +19,7 @@ Estado al cierre:
- `arts/sess/engine-session.ts` delega la clasificacion `none/anonymous/identified` en `session-identity.ts`.
- `arts/sess/engine-session.ts` delega snapshot, generation, dispatch, persistencia y commits en `session-state.ts`.
- `arts/sess/engine-session.ts` delega la resolucion de revoke local/global/degradado en `session-revoke.ts`.
- `arts/conn/connection.ts` delega la programacion de reconnect y exhaustion en `connection-reconnect-runtime.ts`.
- Integracion total ampliada: `Auth.signOut()` valida anonimizacion, invalidacion de `Permissions` y evento `Cache.invalidate`.
- Tanda focalizada verde: `npx vitest run src/arts/conn src/libs/cach src/arts/cach src/svrs/cach src/arts/auth src/svrs/auth src/libs/auth src/arts/aapp/test/ecosystem.integration.test.ts` -> 19 archivos, 82 tests.
- `/test/ecosystem` revisado en navegador: carga sin errores de consola, `ar` cambia a `rtl`, Formats se actualiza por locale, Perm cambia con rol `viewer`, Cach re-scopea por locale y Conn loopback publica/recibe.

@ -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);
}

@ -3,6 +3,7 @@ import { createConnectionAckRegistry } from './acks.ts';
import { wireBrowserReconnect } from './browser-reconnect.ts';
import { createConnectionChannelRegistry } from './channel-registry.ts';
import { createConnectionEventBus } from './connection-events.ts';
import { scheduleConnectionReconnect } from './connection-reconnect-runtime.ts';
import { createConnectionRequestRuntime } from './connection-requests.ts';
import { createConnectionStateTracker } from './connection-state.ts';
import { createConnectionTimerControls } from './connection-timers.ts';
@ -25,7 +26,6 @@ import {
CONNECTION_STATE_OPEN,
CONNECTION_STATE_RECONNECTING,
CONNECTION_TRANSPORT_STATE_OPEN,
LOG_MSG_RECONNECT_EXHAUSTED,
TIMER_KEY_RECONNECT
} from './consts.ts';
import {
@ -217,20 +217,14 @@ export function createConnection<TChannels extends ConnectionChannelMap = Connec
function scheduleReconnect(): void {
if (!shouldReconnect()) return;
const nextReconnect = reconnectPolicy.next(stateTracker.reconnectAttempt);
if (!nextReconnect.ok) {
emitConnectionDiagnostic(diagnostics, CONNECTION_DIAGNOSTIC_EVENTS.RECONNECT_EXHAUSTED, {
reconnectAttempt: nextReconnect.reconnectAttempt,
maxAttempts: nextReconnect.maxAttempts
});
const exhaustedError = new Error(LOG_MSG_RECONNECT_EXHAUSTED);
stateTracker.setError(exhaustedError);
stateTracker.transition(CONNECTION_STATE_FAILED, exhaustedError);
return;
}
stateTracker.setReconnectAttempt(nextReconnect.attempt);
timers.schedule(TIMER_KEY_RECONNECT, nextReconnect.delayMs, () => {
void connectInternal(true);
scheduleConnectionReconnect({
policy: reconnectPolicy,
state: stateTracker,
timers,
diagnostics,
connect: () => {
void connectInternal(true);
}
});
}

Loading…
Cancel
Save

Powered by TurnKey Linux.