Hedged Requests With Abort And Delay

Cut tail latency by launching a second attempt after a delay, cancelling losing attempts and aggregating errors only when all fail.

TypescriptResilience

Code

typescriptsnippet
83 lines
export interface HedgedRequestOptions {
  delayMs: number;
  maxAttempts?: number;
  signal?: AbortSignal;
}

export async function hedgedRequest<T>(
  attempt: (signal: AbortSignal, attemptIndex: number) => Promise<T>,
  options: HedgedRequestOptions,
): Promise<T> {
  if (typeof attempt !== "function") throw new TypeError("attempt must be a function");
  if (!Number.isFinite(options.delayMs) || options.delayMs < 0) {
    throw new RangeError("delayMs must be a non-negative finite number");
  }
  const maxAttempts = options.maxAttempts ?? 2;
  if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
    throw new RangeError("maxAttempts must be a positive integer");
  }
  const external = options.signal;
  if (external?.aborted) throw external.reason;

  return new Promise<T>((resolve, reject) => {
    const controllers = new Set<AbortController>();
    const timers = new Set<ReturnType<typeof setTimeout>>();
    const failures: unknown[] = [];
    let launched = 0;
    let settled = 0;
    let finished = false;

    const cleanup = (): void => {
      for (const timer of timers) clearTimeout(timer);
      timers.clear();
      external?.removeEventListener("abort", onExternalAbort);
      for (const controller of controllers) controller.abort();
      controllers.clear();
    };

    const finish = (callback: () => void): void => {
      if (finished) return;
      finished = true;
      cleanup();
      callback();
    };

    const onExternalAbort = (): void => {
      finish(() => reject(external?.reason ?? new Error("hedged request aborted")));
    };

    const launch = (): void => {
      if (finished || launched >= maxAttempts) return;
      const index = launched;
      launched += 1;
      const controller = new AbortController();
      controllers.add(controller);
      if (external?.aborted) controller.abort(external.reason);

      Promise.resolve()
        .then(() => attempt(controller.signal, index))
        .then(
          (value) => finish(() => resolve(value)),
          (error) => {
            if (finished) return;
            failures.push(error);
            settled += 1;
            if (settled === launched && launched === maxAttempts) {
              finish(() => reject(new AggregateError(failures, "all hedged attempts failed")));
            }
          },
        );

      if (index + 1 < maxAttempts) {
        const timer = setTimeout(() => {
          timers.delete(timer);
          launch();
        }, options.delayMs);
        timers.add(timer);
      }
    };

    external?.addEventListener("abort", onExternalAbort, { once: true });
    launch();
  });
}

How it works

Structurally, expect the functions cleanup, finish, onExternalAbort and launch and nothing else.

The core work runs in nested loops — outer for (const timer of timers) clearTimeout(timer);, inner for (const controller of controllers) controller.abort();. Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. Failure is loud: TypeError and RangeError aborts the current operation instead of degrading it.

A fresh value is allocated for the result; the arguments stay untouched.

What to watch for

  • The guard typeof attempt !== "function" short-circuits the flow with an early throw when its condition holds.

Using it

hedgedRequest is what another module would consume; nothing else here is load-bearing for callers.

Key statements

finish(() => reject(external?.reason ?? new Error("hedged request aborted")));
external?.addEventListener("abort", onExternalAbort, { once: true });
const controllers = new Set<AbortController>();

Interactive Tools for This Topic

FAQ

Does `hedgedRequest` modify its input?
It does not. Inputs are only read; the return value is freshly built.
What happens if the preconditions do not hold?
`TypeError` and `RangeError` is raised at the guard (`typeof attempt !== "function"`), so invalid state never reaches the main logic.
Are there any imports to resolve outside the language?
No. Every import resolves against the language's own modules. The file defines `cleanup`, `finish`, `onExternalAbort` at load time.