# Add Request Timeouts to an Actix Web App

Yes. An agent adds a per-request timeout middleware and server-level client timeouts to your Actix Web app, tests the cut-off with a slow handler, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-request-timeouts/actix/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the server before it adds a deadline anywhere. In the `basics/todo` example in actix/examples, `main.rs` builds an `HttpServer` with `actix-web` `4.11` from the workspace `Cargo.toml` (edition 2024, `rust-version` 1.98), wraps the app in `Logger`, a cookie `SessionMiddleware` and `ErrorHandlers` that serve `static/errors/400.html`, `404.html` and `500.html`, registers `/`, `POST /todo` and `POST /todo/{id}`, mounts `/static`, and binds `127.0.0.1:8080` with two workers. `db.rs` opens the SQLite pool through `sqlx` `0.9` with `acquire_timeout` of one second, which is the only deadline the app has today: a handler that gets a connection can then run as long as it likes, and a client that sends headers slowly holds a worker's connection with nothing stopping it. `.env` sets `SQLX_OFFLINE=true` and the `.sqlx` directory holds the query metadata, so the crate builds without a live database.

The repository already shows the shape: `middleware/various/src/main.rs` wraps `next.call(req)` in `actix_web::rt::time::timeout` from a `from_fn` middleware. The agent writes the same thing for the todo app as `src/timeout.rs`, with the duration read from `REQUEST_TIMEOUT_SECS` through `dotenvor` like `DATABASE_URL` is, applied with `.wrap(from_fn(request_timeout))` so it covers the handlers but not the error pages. When the deadline passes the middleware drops the handler's future, which cancels a pending `sqlx` query, and answers `503 Service Unavailable` rather than `408`, because the client did nothing wrong. A fourth `ErrorHandlers` entry serves a new `static/errors/503.html` in the style of the other three.

The server gets its own limits in the same change: `client_request_timeout` for how long a client may take to send the request head, `client_disconnect_timeout` for the shutdown of a connection, `keep_alive` for idle connections, and `shutdown_timeout` so a deploy does not wait forever on a worker. The handler timeout is set longer than the pool's one-second `acquire_timeout`, so a busy pool still produces the pool's own error instead of a confusing cut-off, and the README's run instructions gain the new variable.

Then it proves it. A test module mounts the middleware around a handler that sleeps past the deadline and asserts the `503` with `actix_web::test::init_service` and `call_service`, and a second test shows a fast handler is unaffected. `cargo test -p todo` runs with `SQLX_OFFLINE=true` from `.env`, `cargo clippy --workspace --all-targets --all-features` matches the `justfile`, and `cargo +nightly fmt` matches `clippy-fmt.yml`. The agent also starts the app after `sqlx database create` and `sqlx migrate run`, the steps the README gives, and shows with `curl` that `/` still renders the task list.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add request timeouts to this Actix Web app. Read src/main.rs, src/api.rs, src/db.rs, Cargo.toml, .env, the README and the middleware/various example in this workspace first. Write a from_fn middleware in src/timeout.rs that wraps next.call(req) in actix_web::rt::time::timeout with a duration read from REQUEST_TIMEOUT_SECS, answers 503 when it fires, and is registered so it covers the handlers; add a 503 error page served through ErrorHandlers like the existing 400, 404 and 500 pages. Set client_request_timeout, client_disconnect_timeout, keep_alive and shutdown_timeout on the HttpServer, and keep the handler timeout longer than the pool's acquire_timeout. Write tests with actix_web::test that a handler sleeping past the deadline gets 503 and a fast handler is unaffected. Run cargo test for this crate with SQLX_OFFLINE=true, cargo clippy and cargo fmt as the workspace does, start the app after the sqlx setup steps in the README and show with curl that the index still renders. Open a pull request documenting the new variable.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch with `src/timeout.rs`, the middleware registration and the server settings in `src/main.rs`, the new `static/errors/503.html`, the `REQUEST_TIMEOUT_SECS` line in `.env` and the README, and the tests. `api.rs`, `db.rs`, `model.rs` and the migrations are untouched, and the `.sqlx` metadata needs no regeneration because no query changed.

The description quotes the test run with both cases, the clippy and fmt results, and the `curl` of the index after the app started against a freshly migrated database. The transcript at `GET /o/{org}/sessions/{id}/messages` keeps every command with its output, including the `sqlx` setup and the server log line for the cut-off request.

Before merging, a reviewer sets the production value of `REQUEST_TIMEOUT_SECS` against the slowest legitimate request, checks that the load balancer's own timeout is longer than it so the client sees the app's `503` page rather than a proxy error, and decides whether the `keep_alive` value suits the proxy in front. A follow-up message to the same session changes any of them.

## Run it on every service put behind a load balancer

A developer who is about to put a service behind a load balancer starts one run from the terminal with this prompt and the repository, and comes back to a pull request whose tests show the deadline firing. The run's sandbox has the Rust toolchain and the SQLite database the README describes, so nothing is simulated.

A platform team can send one `POST /o/{org}/sessions` per Actix service, with `GET /o/{org}/github` listing the repositories the installation reaches. Each run reads that service's own `HttpServer` builder and pool settings, so a service that already sets `client_request_timeout` gets only the handler middleware and a note saying why.

## Why run it on Rush?

A timeout is a claim about behaviour under load, and the only honest test is a request that actually outlives it. The agent writes that test with the framework's own test service, runs it in its own sandbox with the real `sqlx` setup, and keeps the output. It also reads the pool's `acquire_timeout` before choosing a value, which is the detail that turns a reasonable change into a confusing one when the two are reversed.

The pull request carries the proof: the test output, clippy, fmt and the `curl` of a running server are in the transcript beside the commands that produced them. Several services can be handled at once, each in its own sandbox, and the sandbox parks when the turn ends so a follow-up that tunes a value wakes it with the build cached.

## Frequently asked questions

### Why 503 and not 408 when the handler is too slow?

Because `408 Request Timeout` means the client took too long to send its request, which is what `client_request_timeout` on the server handles. A handler that runs past the deadline is the server's problem, so the middleware answers `503 Service Unavailable` and serves an error page in the style of the existing `400`, `404` and `500` pages.

### What happens to a database query when the timeout fires?

The middleware drops the handler's future, so a pending `sqlx` query is cancelled with it and the connection returns to the pool. The handler timeout is set longer than the pool's one-second `acquire_timeout`, so a busy pool still surfaces as the pool's own error rather than a cut-off.

### Does the change need the database to build?

No. `.env` sets `SQLX_OFFLINE=true` and the `.sqlx` directory already holds the query metadata, so `cargo test` and `cargo clippy` run without SQLite. The agent still creates and migrates the database with the `sqlx` commands from the README to start the app and check the index renders.

### Will the timeout apply to static files and error pages?

The middleware is registered so it wraps the handlers, and the `Files` service and the `ErrorHandlers` responses are not where time is lost. The pull request shows the registration order in `main.rs` so a reviewer can move it if they want static files covered too.

## More Actix Web jobs

- [Add Security Headers to an Actix Web App](https://getrush.ai/run/add-security-headers/actix/)
- [Add Soft Delete to an Actix Web API](https://getrush.ai/run/add-soft-delete/actix/)
- [Add Structured Logging to an Actix Web App](https://getrush.ai/run/add-structured-logging/actix/)

## The same job on other stacks

- [Add Request Timeouts to an aiohttp App](https://getrush.ai/run/add-request-timeouts/aiohttp/)
- [Add Request Timeouts to an Axios API Client](https://getrush.ai/run/add-request-timeouts/axios/)
- [Add Request Timeouts to an Axum API](https://getrush.ai/run/add-request-timeouts/axum/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
