/** * Hedged requests — fire the same call to multiple endpoints, accept * the first successful response, abort the rest. Reduces P99 latency * dramatically (Google's "tail at scale": typical 75-96% drop) at a * 5-10% extra cost. * * @example * ```ts * import { hedge } from "misina/hedge" * * const data = await hedge(misina, "/inference", { * endpoints: [ * "https://us-east.api.example.com", * "https://us-west.api.example.com", * ], * delayMs: 200, // wait 200ms before firing the second * max: 2, // cap parallel attempts * }) * ``` */ import type { Misina, MisinaRequestInit } from "../types.mjs"; export interface HedgeOptions { /** * Endpoint base URLs. Each request is dispatched against * `endpoint + path`. Order matters — earlier endpoints fire first. */ endpoints: string[]; /** * Delay in ms between firing the next endpoint. Default: 0 * (all endpoints fire immediately in parallel). */ delayMs?: number; /** Cap on concurrent attempts. Default: endpoints.length. */ max?: number; /** Per-call init forwarded to misina. */ init?: MisinaRequestInit; /** External AbortSignal — cancels all in-flight attempts. */ signal?: AbortSignal; } /** * Race a request across endpoints; first successful response wins. * Aborts all losing in-flight attempts. If every endpoint errors, the * first error is rethrown (others surface on `error.cause` array). * * @returns the data from the winning response. */ export declare function hedge(misina: Misina, path: string, options: HedgeOptions): Promise; export declare class HedgeLoserError extends Error { override readonly name = "HedgeLoserError"; constructor(reason: string); }