/** * Polling utilities with hybrid polling strategy: fast initial polls followed * by exponential backoff with proportional jitter. * * The default strategy polls at 1-second intervals for the first 30 seconds, * then gradually increases the interval using exponential backoff (1.15x multiplier) * up to a maximum of 30 seconds between polls. * * @example * ```typescript * // Polls until complete (uses hybrid strategy by default) * const result = await pollUntilDone( * () => client.extractRuns.retrieve(id), * (res) => res.extractRun.status !== "PROCESSING" * ); * * // With custom timeout * const result = await pollUntilDone( * () => client.extractRuns.retrieve(id), * (res) => res.extractRun.status !== "PROCESSING", * { maxWaitMs: 300000 } // 5 minute timeout * ); * * // Custom fast polling phase * const result = await pollUntilDone( * () => client.extractRuns.retrieve(id), * (res) => res.extractRun.status !== "PROCESSING", * { * fastPollDurationMs: 60000, // Fast poll for 60 seconds * fastPollIntervalMs: 500, // Poll every 500ms during fast phase * } * ); * ``` */ export interface PollingOptions { /** * Maximum total wait time in milliseconds. * @default undefined (polls indefinitely) */ maxWaitMs?: number; /** * Duration of the fast polling phase in milliseconds. * During this phase, polls occur at a fixed interval (fastPollIntervalMs). * @default 30000 (30 seconds) */ fastPollDurationMs?: number; /** * Interval between polls during the fast polling phase in milliseconds. * @default 1000 (1 second) */ fastPollIntervalMs?: number; /** * Initial delay for the backoff phase (after fast polling ends) in milliseconds. * @default 1000 (1 second) */ initialDelayMs?: number; /** * Maximum delay between polls in milliseconds. * @default 30000 (30 seconds) */ maxDelayMs?: number; /** * Multiplier for exponential backoff during the backoff phase. * A value of 1.15 means each delay is 1.15x the previous delay. * @default 1.15 */ backoffMultiplier?: number; /** * Jitter fraction for randomization. A value of 0.25 means delays * will be randomized by +/-25%. * @default 0.25 */ jitterFraction?: number; } /** * Error thrown when polling exceeds the maximum wait time. */ export declare class PollingTimeoutError extends Error { readonly elapsedMs: number; readonly maxWaitMs: number; constructor(message: string, elapsedMs: number, maxWaitMs: number); } /** * Calculates the next delay using exponential backoff with proportional jitter. * * @param attempt - The current attempt number (0-indexed) * @param initialDelayMs - The base delay for the first attempt * @param maxDelayMs - The maximum delay cap * @param jitterFraction - The fraction of delay to randomize (+/-) * @param backoffMultiplier - The multiplier for exponential backoff (default: 2) * @returns The delay in milliseconds */ export declare function calculateBackoffDelay(attempt: number, initialDelayMs: number, maxDelayMs: number, jitterFraction: number, backoffMultiplier?: number): number; /** * Configuration options for calculating hybrid delay. */ export interface HybridDelayOptions { /** Duration of fast polling phase in milliseconds */ fastPollDurationMs: number; /** Interval between polls during fast phase in milliseconds */ fastPollIntervalMs: number; /** Initial delay for backoff phase in milliseconds */ initialDelayMs: number; /** Maximum delay cap in milliseconds */ maxDelayMs: number; /** Multiplier for exponential backoff */ backoffMultiplier: number; /** Jitter fraction for randomization */ jitterFraction: number; } /** * Calculates the delay for a hybrid polling strategy based on elapsed time. * * During the fast polling phase (elapsed < fastPollDurationMs), returns a fixed * interval with jitter. After the fast phase ends, switches to exponential backoff. * * @param elapsedMs - Total elapsed time since polling started * @param options - Configuration options for the hybrid strategy * @returns The delay in milliseconds until the next poll */ export declare function calculateHybridDelay(elapsedMs: number, options: HybridDelayOptions): number; /** * Polls a retrieve function until a terminal condition is met. * * Uses a hybrid polling strategy: fast polling at fixed intervals for an initial * period, then exponential backoff with proportional jitter. This provides low * latency for quick operations while still reducing server load for longer ones. * * Default behavior: * - Fast phase: Poll every 1 second for the first 30 seconds * - Backoff phase: Exponential backoff with 1.15x multiplier, max 30 second delay * * By default, polls indefinitely until a terminal state is reached. * Use `maxWaitMs` to set an explicit timeout if desired. * * @param retrieve - Async function that fetches the current state * @param isTerminal - Predicate that returns true when polling should stop * @param options - Polling configuration options * @returns The final result when isTerminal returns true * @throws {PollingTimeoutError} If maxWaitMs is set and exceeded * * @example * ```typescript * // Polls indefinitely (suitable for development/testing) * const result = await pollUntilDone( * () => client.extractRuns.retrieve(runId), * (res) => res.extractRun.status !== "PROCESSING" * ); * * // With timeout * const result = await pollUntilDone( * () => client.extractRuns.retrieve(runId), * (res) => res.extractRun.status !== "PROCESSING", * { maxWaitMs: 300000 } // 5 minute timeout * ); * * // Pure exponential backoff (disable fast polling phase) * const result = await pollUntilDone( * () => client.extractRuns.retrieve(runId), * (res) => res.extractRun.status !== "PROCESSING", * { fastPollDurationMs: 0 } * ); * ``` */ export declare function pollUntilDone(retrieve: () => Promise, isTerminal: (result: T) => boolean, options?: PollingOptions): Promise;