Retry-After and a time limit
Honor a Retry-After header, and limit how long each attempt and the whole call may take.
A server that's overloaded can ask you to wait before you try again, and a
slow one can keep a call waiting far longer than your app should. retry
and timeout handle both: retry can wait as long as the server asks, and
the two plugins set time limits on each attempt and on the whole call. This
page shows how to use each limit, how to combine them, and what they don't
cover.
Wait as Retry-After asks
To wait as long as a server's Retry-After header asks, in place of the
retry schedule's delay, pass retryAfter:
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
export class extends .<>()("Api", {
: "https://api.example.com",
: [({ : 5, : { : "30 seconds" } })],
}) {}retryAfter: true waits up to 1 minute, and { max } up to max. It's
off by default, so a server can't make your calls wait unless you allow it.
retry reads the header from a response it's about to retry, such as a
429 or 503. The header can hold whole seconds (Retry-After: 120) or an
HTTP date. Each case works as follows:
- With no header, or one that
retrycan't read, the schedule's delay is kept. - A date in the past retries at once.
- A wait longer than the cap is cut to the cap. The retry still happens.
Limit each attempt with timeout
timeout fails an attempt with a TimeoutError, and aborts its request,
when the response headers don't arrive in time. Put it after retry, so
each attempt gets the full duration:
import { } from "@monarcode/effetch";
import { , } from "@monarcode/effetch/plugins";
export class extends .<>()("Api", {
: "https://api.example.com",
: [(), ({ : "10 seconds" })],
}) {}In that position, the limit is per attempt, and an attempt that times out
isn't retried: the call fails with the TimeoutError. Placed before retry,
one timeout would cover every attempt and wait together. To limit the
whole call, use a budget instead, which the next section shows.
Limit the whole call with a budget
To limit how long the whole call may take, every attempt and every wait
between them included, give retry a budget:
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
export class extends .<>()("Api", {
: "https://api.example.com",
: [({ : 5, : true, : "30 seconds" })],
}) {}The budget works as follows:
- A retry that wouldn't fit isn't made. When the wait before the next
retry would end at or after the budget,
retrystops there. The call ends as it does when the retries run out: with the last response, or the lastNetworkError. - An attempt that runs out of budget is aborted. The call then fails
with a
TimeoutError, the same classtimeoutuses. - Calls that aren't retried are limited too, such as a
POST, or a call withtimes: 0.
The budget ends when the last response's headers arrive. Like timeout, it
doesn't cover reading the body (see
What the limits don't cover).
Combine a per-attempt and a whole-call limit
For "10 seconds per attempt, 30 seconds in all", use both timeout and a
budget:
import { } from "@monarcode/effetch";
import { , } from "@monarcode/effetch/plugins";
export class extends .<>()("Api", {
: "https://api.example.com",
: [
({ : 5, : true, : "30 seconds" }),
({ : "10 seconds" }),
],
}) {}Both fail with a TimeoutError from @monarcode/effetch/plugins. It has the
request's method, its url without the query string, and the duration
that ran out, formatted, such as "10s" or "30s".
Give one call a different limit
A call can change either limit in its plugins option:
import * as from "effect/Effect";
import { } from "@monarcode/effetch";
import { , } from "@monarcode/effetch/plugins";
class extends .<>()("Api", {
: "https://api.example.com",
: [
({ : 5, : "30 seconds" }),
({ : "10 seconds" }),
],
}) {}
export const = .(function* () {
const = yield* ;
return yield* .("/report", {
: {
: { : "2 minutes" },
: { : "1 minute" },
},
});
});A call can override the budget only when the client's retry has one. A
plain retry() takes only { times } per call, and TypeScript rejects a
budget. To let calls set a budget without one for every call, pass a
budget whose type is Duration.Input | undefined, such as a value from
your config:
import type * as from "effect/Duration";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
declare const : { readonly : . | undefined };
export class extends .<>()("Api", {
: "https://api.example.com",
: [({ : . })],
}) {}
// Per call: api.get("/report", { plugins: { retry: { budget: "2 minutes" } } })When the value is undefined, there's no limit until a call sets one. A
literal budget: undefined doesn't work for this: TypeScript reads it as a
plain retry().
What the limits don't cover
timeout and the budget both stop when the response headers arrive. Reading
and decoding the body come after that, so a large response that starts in
time isn't cut off while it downloads. For a stream from api.sse or
api.ndjson, they cover getting the response, and the stream then runs
without a limit.
To cap the whole call, the body included, wrap a call that decodes its body
in Effect.timeout:
import * as from "effect/Effect";
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : .(.) });
export const =
.("/report", { : })
.(.("1 minute"));The cap covers the body because the call reads it: with output, the call
ends after decoding. A call without output returns the response when the
headers arrive, so its timeout ends there too.
Effect.timeout interrupts the call, which aborts its request, and fails
with Effect's own TimeoutError, from effect/Cause. That error has the
same "TimeoutError" tag as Effetch's, but no method, url or
duration, so Effect.catchTag("TimeoutError", …) catches both. On a
client without retry, Effect.timeout is also the way to limit one call
in all.
What's next
- Change plugins for one call: give one call a
longer budget or timeout, or skip
retry. - Built-in plugins: see why
timeoutgoes afterretry. - Catch errors: handle a
TimeoutErrorwhere you make the call.