import { type MaintenanceNotification } from "./types"; import type { HandoffCandidate, HandoffEndpoint } from "../redis/ConnectionSession"; /** * Windows normally close when the server sends the matching end notification * (MIGRATED / FAILED_OVER). The caps below only guard against an end * notification that never arrives; the values follow the operation durations * suggested in the HLD (10 minutes for a shard migration, 1 minute for a * shard failover). */ export declare const MIGRATING_WINDOW_CAP_MS: number; export declare const FAILING_OVER_WINDOW_CAP_MS: number; /** * MOVING has no end notification. The server hard-closes the connection when * the grace period communicated in the notification is over, so the window * expires on its own shortly after that deadline. */ export declare const MOVING_WINDOW_MARGIN_MS = 2000; export interface TimeoutPolicy { timeout: number; createTimeoutError: () => Error; } export interface MaintenanceTimeoutOptions { commandTimeout?: number; socketTimeout?: number; maintRelaxedCommandTimeout?: number; maintRelaxedSocketTimeout?: number; } /** Connection operations supplied by Redis without exposing its mutable state. */ export interface MaintenanceConnection { readonly options: MaintenanceTimeoutOptions; extendPendingCommandTimeouts(policy: TimeoutPolicy): void; rearmSocketTimeout(): void; flushOfflineQueue(): void; canHandoff(): boolean; getConfiguredEndpoint(): HandoffEndpoint | null; createCandidate(endpoint: HandoffEndpoint): Promise; waitForDrain(): Promise; /** Validate and transfer ownership before disposing the old connection. */ commitHandoff(candidate: HandoffCandidate, endpoint: HandoffEndpoint): void; } /** * Owns the client-side Smart Client Handoff state: maintenance windows * announced through RESP3 push notifications, the timeout policy that * applies while they are open, and the handoff write pause. * * A window opens on MIGRATING, FAILING_OVER, or MOVING and closes on the * matching end notification, on its deadline, or when the connection drops. */ export default class MaintenanceManager { private readonly connection; private readonly windows; private writePause; private movingReconnectTimer; constructor(connection: MaintenanceConnection); handle: (notification: MaintenanceNotification) => void; isMaintenanceActive(): boolean; /** * The relaxed command timeout that applies while maintenance is active, or * null when the normal timeout applies. */ commandTimeoutPolicy(): TimeoutPolicy | null; /** * The relaxed socket timeout that applies while maintenance is active, or * null when the normal timeout applies. */ socketTimeoutPolicy(): TimeoutPolicy | null; /** * Pauses writes for a Smart Client Handoff. While paused, submitted * commands are retained in the offline queue instead of being written to * the connection. Returns a token that must be presented to resume writes; * only one handoff may be active at a time. */ pauseWrites(): symbol; /** * Resumes writes after a handoff settles and replays the offline queue. * A stale token (from a handoff that was already superseded or cleaned up * by a disconnect) is ignored, so late asynchronous cleanup cannot unpause * a newer handoff. */ resumeWrites(token: symbol): void; isWritePaused(): boolean; /** * Clears every window and any write pause. Invoked when the connection * closes: if maintenance is still ongoing, the server re-sends the pending * notification on the next connection, and the ordinary reconnect path * replays the offline queue. */ reset(): void; /** * Ends maintenance after a connection loss and reports whether its WATCH * was lost during maintenance. Redis records that result on the client so * a later transaction cannot silently proceed without its optimistic lock. */ handleConnectionClose(watching: boolean): boolean; /** * A repeated start notification refreshes the window instead of stacking: * the server replaces its pending notification, so the latest deadline wins. */ private openWindow; private closeWindow; private clearTimer; /** * MOVING opens a relaxation window like the other notifications and, when * the connection shape allows it, starts a Smart Client Handoff to the * announced endpoint. */ private handleMoving; /** * An endpointless MOVING asks the client to move back to its configured * endpoint, whose DNS record is being repointed. Wait half the grace * period (giving DNS time to flip) and then hand off proactively while * the old endpoint is still serving, rather than waiting for the abrupt * server-side close at the deadline. If the handoff cannot run or fails, * that hard-close remains the backstop through the ordinary reconnect * path. */ private scheduleEndpointlessHandoff; private clearMovingReconnectTimer; /** * The sequential handoff: pause writes, connect the replacement and drain * the old connection in parallel, then atomically adopt the replacement * and replay the paused commands. On any failure the old connection keeps * (or regains) ownership: writes resume if it is still healthy, and if it * is gone, the close path has already cleared the pause so the ordinary * reconnect replays the offline queue. */ private performHandoff; private withGraceDeadline; /** * Invoked when the first window opens. In-flight commands get their * deadlines extended to the relaxed timeout (never shortened) and a * pending socket timeout is re-armed with the relaxed value. New commands * and future socket arms consult the timeout policies themselves. */ private relaxTimeouts; /** * Invoked when the last window closes. Existing commands keep the timeout * assigned to them during maintenance, while a pending socket timeout is * re-armed with the normal value. */ private restoreSocketTimeout; }