Skip to content

Per-attempt timeout or total deadline?

One request goes to a host that hangs and never answers. Both clients wait 2 seconds per attempt and allow 3 retries. Only the right one also sets a total deadline of 4 seconds.

Each row is one attempt. Press Play: the page slows down at key moments and says what happened.

t = 0.0 s

First attemptRetryBackoff waitAttempt ends: timeout or abortHost still working on itTotal deadline

Per-attempt timeout only

timeout: { attemptMs: 2000 }
0.0 scaller has waited
0attempts sent
waitingerror the caller gets
0.0 shost time on hung attempts

Per-attempt timeout + total deadline

timeout: { attemptMs: 2000, totalMs: 4000 }
0.0 scaller has waited
0attempts sent
waitingerror the caller gets
0.0 shost time on hung attempts

When the caller hears back, by totalMs

The flat line ending in a square is the client with no deadline. The rising line is the client with a deadline, and its dot marks your current totalMs. Past the flat line, the deadline no longer helps.

Host time spent on hung attempts

Each attempt keeps the host busy while it waits. Aborting the attempt does not stop the host at once. In this model it works 6 more seconds.

What each timeout gives you

  1. attemptMs caps one attempt. A hung host costs at most attemptMs per try, and the attempt ends with a TimeoutError that Vereda retries. The timer also covers the body read and an async parse, when parse or responseType is set.
  2. totalMs caps the whole request. The clock starts at request() and counts everything: queue waits, rate-limit waits, attempts and backoff sleeps. At the deadline, Vereda aborts the attempt or wakes the sleep, and the ticket ends with DeadlineExceededError.
  3. Doomed retries are skipped. If the next backoff would reach the deadline, the ticket fails now instead of sleeping first, with the last TimeoutError as cause. Set totalMs to 7 s: attempt #3 times out at 6.6 s, the 0.8 s backoff would reach 7 s, so the right lane stops right then.

What they cost

  • A short attemptMs also cuts requests that were only slow. Each cut becomes a retry, and retries add load to a host that may already be struggling.
  • The deadline can cut an attempt that was about to answer. Vereda compares only the backoff delay with the deadline, not delay plus attemptMs, so a retry that answers fast still fits.
  • Aborting is a client-side act. The host may keep working on the request. Vereda retries timeouts only for idempotent requests, so this is safe to repeat.
  • attemptMs: Infinity removes the cap. Move the slider to the end and the left lane never answers. Only cancel() or totalMs can end the wait.

Things to try:

  • Set totalMs to 1 s: the right lane aborts the first attempt.
  • Set max retries to 0: the caller gets a plain TimeoutError with no wrapper.
  • Turn both timeouts off: neither lane ever answers.

In Vereda

import { HttpClient } from "@vereda/http"; const client = HttpClient.create({ timeout: { attemptMs: 2_000, // required, or Infinity totalMs: 4_000, // optional deadline }, retry: { maxRetries: 3 }, }) // per request: client.get("/slow", { timeout: { totalMs: 10_000 }, })

This page follows the logic in three files:

  • src/queue/retry.ts: the deadline check before each backoff, and the abort path.
  • src/queue/executor.ts: the attempt timer and the body read.
  • src/core/client.ts: the deadline timer, armed in send().

The errors are in src/core/errors.ts. TimeoutError has kind: "timeout", DeadlineExceededError has kind: "deadline" and MaxRetriesExceededError has kind: "max_retries".

A test, test/demos/timeouts.parity.test.ts, checks this page's model against the real HttpClient: when each attempt starts and ends, when the caller hears back, and which error it gets. The page has to change when the library does.

The host's 6 s of extra work is invented for the chart, and no test checks it.

Model choices: backoff is 200 ms, 400 ms, 800 ms and so on, with jitter off so the numbers repeat. Vereda's default has full jitter, so each real wait is a random value up to that size.

Finite timeouts must fit in 231-1 ms. Infinity takes its own path in src/core/timer.ts, so it never fires early (#203).

A user cancel() ends the ticket at once with CancelledError and also aborts the attempt.