/** * Internal RPC state for a single request. */ interface RpcRequestState { cancelled: boolean; terminalSent: boolean; onCancelCallbacks: (() => void)[]; abortController: AbortController; createdAt: number; lastActivityAt: number; } /** * Internal RPC state manager. * * Encapsulates RPC lifecycle management (request tracking, cancellation, * idle cleanup, inflight rate limiting). Keeps router focused on message * routing and validation. * * @internal */ export declare class RpcManager { private readonly statesByClient; private readonly inflightByClient; private readonly recentlyTerminated; private readonly MAX_RECENTLY_TERMINATED; private idleCleanupHandle; private readonly maxInflightPerSocket; private readonly idleTimeoutMs; private readonly cleanupCadenceMs; private dedupWindowMs; constructor(options?: { maxInflightPerSocket?: number; idleTimeoutMs?: number; cleanupCadenceMs?: number; dedupWindowMs?: number; }); /** * Called when an RPC request starts (before dispatch to handler). * Reserves an inflight slot and tracks the request. * * @returns true if inflight limit allows, false if limit exceeded */ onRequest(clientId: string, correlationId: string): boolean; /** * Called when progress is sent to update activity timestamp. * This prevents idle cleanup from removing active RPCs. */ onProgress(clientId: string, correlationId: string): void; /** * Called when a terminal message (reply or error) is sent. * Marks request as terminated (one-shot guard) and schedules cleanup. * * @returns true if terminal was accepted, false if already terminal */ onTerminal(clientId: string, correlationId: string): boolean; /** * Called when an abort message is received from the client. * Cancels all registered callbacks without sending a response. */ onAbort(clientId: string, correlationId: string): void; /** * Called when a socket disconnects. * Cancels all in-flight RPCs and clears socket state. */ onDisconnect(clientId: string): void; /** * Check if an RPC has been terminated (reply or error already sent). * Checks both current state and recently-terminated tracking. */ isTerminal(clientId: string, correlationId: string): boolean; /** * Register a callback to be called when RPC is cancelled. * * @returns Unregister function to remove the callback */ onCancel(clientId: string, correlationId: string, callback: () => void): () => void; /** * Start the idle cleanup timer. * Removes RPC states that have not had activity for idleTimeoutMs. */ start(): void; /** * Stop the idle cleanup timer (e.g., when router shuts down). */ stop(): void; /** * Set the deduplication window for RPC requests (in milliseconds). * Used to control how long completed RPC IDs are remembered for duplicate detection. * Primarily useful for testing to verify cleanup with shorter durations. * * @internal Testing only; production code should configure via RpcManager constructor options * @param dedupWindowMs Deduplication window in milliseconds (e.g., 100 for fast test cleanup) * * @example * ```typescript * const manager = new RpcManager({ dedupWindowMs: 3600_000 }); * // In tests, override for faster cleanup: * manager.setDedupWindow(100); // Speed up dedup window to 100ms * ``` */ setDedupWindow(dedupWindowMs: number): void; /** * Clean up RPCs that have been idle for idleTimeoutMs. * @internal */ private cleanupIdle; /** * Get or create RPC state for a correlation ID. * @internal */ private getOrCreateState; /** * Remove RPC state (cleanup after terminal or timeout). * @internal */ private prune; /** * Get the AbortSignal for an RPC request. * Always returns a valid AbortSignal (pre-aborted if state doesn't exist). * This ensures handlers always have a real signal to work with. * @internal */ getAbortSignal(clientId: string, correlationId: string): AbortSignal; /** * Get internal RPC state for testing purposes only. * @internal */ _getState(clientId: string, correlationId: string): RpcRequestState | undefined; } export {}; //# sourceMappingURL=rpc-manager.d.ts.map