/** * Routing Strategies * * When a service has multiple workers, the proxy needs to decide * which worker handles each request. Different strategies suit * different service patterns. * * Strategies: * * round-robin Default. Cycles through workers sequentially. * Best for: stateless services, uniform workloads. * * hash Routes by a key (e.g., userId). Same key always * goes to the same worker. Uses consistent hashing * so adding/removing workers only remaps ~1/N keys. * Best for: stateful services, in-memory caches, * services that benefit from data locality. * * least-pending Routes to the worker with fewest in-flight requests. * Best for: variable-latency services (DB queries, * external API calls). * * broadcast Sends to ALL workers. Returns the first response. * Best for: search/aggregation across sharded data. * * primary Always routes to worker 0. Others are standbys. * Best for: singleton services (cron scheduler, * leader election). */ interface WorkerEntry { key: string; socket: unknown; pending: number; } interface CallContext { __forge_args?: unknown[]; __forge_hash?: string | number; [key: string]: unknown; } interface HashStrategyOptions { key?: string; vnodes?: number; salt?: string; } interface RoutingStrategy { pick(workers: WorkerEntry[], callContext?: CallContext | null): WorkerEntry | WorkerEntry[] | null; readonly name: string; } export declare class RoundRobinStrategy implements RoutingStrategy { index: number; constructor(); pick(workers: WorkerEntry[], _callContext?: CallContext | null): WorkerEntry | null; get name(): string; } export declare class HashStrategy implements RoutingStrategy { keyField: string | null; vnodes: number; private _ring; private _workerCacheKey; private _ringHashes; private _ringKeys; private _fallbackCounter; private _salt; constructor(options?: HashStrategyOptions); pick(workers: WorkerEntry[], callContext?: CallContext | null): WorkerEntry | null; private _extractKey; private _buildRing; private _findOnRing; /** * Pre-compute a keyed SHA-256 hash for ring building (called at build time). */ private _computeHash; /** * P-1: Fast FNV-1a hash for runtime routing decisions. * Salted to prevent predictable distribution from external input. */ private _simpleHash; get name(): string; } export declare class LeastPendingStrategy implements RoutingStrategy { private _pending; private _lastWorkerKeys; constructor(); /** * Pick the worker with the fewest in-flight requests. */ pick(workers: WorkerEntry[], _callContext?: CallContext | null): WorkerEntry | null; /** Call when dispatching a request to a worker */ acquire(workerKey: string): void; /** Call when a request to a worker completes */ release(workerKey: string): void; get name(): string; } export declare class BroadcastStrategy implements RoutingStrategy { /** * Returns ALL workers. The caller is responsible for sending * to all of them and handling multiple responses. */ pick(workers: WorkerEntry[], _callContext?: CallContext | null): WorkerEntry[]; get name(): string; get isBroadcast(): boolean; } export declare class PrimaryStrategy implements RoutingStrategy { /** * Always routes to worker index 0. If worker 0 is down, * routes to the next available. */ pick(workers: WorkerEntry[], _callContext?: CallContext | null): WorkerEntry | null; get name(): string; } /** * Create a routing strategy by name. */ export declare function createStrategy(name: string, options?: HashStrategyOptions): RoutingStrategy; export {}; //# sourceMappingURL=RoutingStrategy.d.ts.map