# Retry-After and a time limit (https://effetch.monarcode.dev/docs/plugins/retry-after)

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`:

```ts
import { Fetch } from "@monarcode/effetch";
import { retry } from "@monarcode/effetch/plugins";

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [retry({ times: 5, retryAfter: { max: "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 `retry` can'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:

```ts
import { Fetch } from "@monarcode/effetch";
import { retry, timeout } from "@monarcode/effetch/plugins";

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [retry(), timeout({ duration: "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`:

```ts
import { Fetch } from "@monarcode/effetch";
import { retry } from "@monarcode/effetch/plugins";

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [retry({ times: 5, retryAfter: true, budget: "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, `retry` stops there. The call ends
  as it does when the retries run out: with the last response, or the last
  `NetworkError`.
- **An attempt that runs out of budget is aborted.** The call then fails
  with a `TimeoutError`, the same class `timeout` uses.
- **Calls that aren't retried are limited too,** such as a `POST`, or a call
  with `times: 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](https://effetch.monarcode.dev/docs/plugins/retry-after#what-the-limits-dont-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:

```ts
import { Fetch } from "@monarcode/effetch";
import { retry, timeout } from "@monarcode/effetch/plugins";

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [
    retry({ times: 5, retryAfter: true, budget: "30 seconds" }),
    timeout({ duration: "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:

```ts
import * as Effect from "effect/Effect";

import { Fetch } from "@monarcode/effetch";
import { retry, timeout } from "@monarcode/effetch/plugins";

class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [
    retry({ times: 5, budget: "30 seconds" }),
    timeout({ duration: "10 seconds" }),
  ],
}) {}

export const report = Effect.gen(function* () {
  const api = yield* Api;
  return yield* api.get("/report", {
    plugins: {
      retry: { budget: "2 minutes" },
      timeout: { duration: "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:

```ts
import type * as Duration from "effect/Duration";

import { Fetch } from "@monarcode/effetch";
import { retry } from "@monarcode/effetch/plugins";

declare const config: { readonly budget: Duration.Input | undefined };

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [retry({ budget: config.budget })],
}) {}

// 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`:

```ts
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

const Report = Schema.Struct({ rows: Schema.Array(Schema.String) });

export const report = api
  .get("/report", { output: Report })
  .pipe(Effect.timeout("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](https://effetch.monarcode.dev/docs/plugins/per-call): give one call a
  longer budget or timeout, or skip `retry`.
- [Built-in plugins](https://effetch.monarcode.dev/docs/plugins#how-plugins-nest): see why `timeout` goes
  after `retry`.
- [Catch errors](https://effetch.monarcode.dev/docs/errors/catching): handle a `TimeoutError` where you
  make the call.