# Add Rate Limiting to a Leptos Server

Yes. An agent adds per-client rate limiting to the Axum server behind your Leptos app with tower_governor, scoped to server functions, tested, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-rate-limiting/leptos/). 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 the limiter. In a project generated from leptos-rs/start-axum, `Cargo.toml` pins `leptos`, `leptos_router`, `leptos_meta` and `leptos_axum` at 0.8.0 with `axum` 0.8.0 and `tokio` behind the `ssr` feature, `wasm-bindgen` 0.2.106 behind `hydrate`, and a `[package.metadata.leptos]` block with `site-addr = "127.0.0.1:3000"`, `reload-port = 3001`, `style-file = "style/main.scss"` and `end2end-cmd = "npx playwright test"`. `src/main.rs` builds `Router::new().leptos_routes(&leptos_options, routes, shell).fallback(leptos_axum::file_and_error_handler(shell)).with_state(leptos_options)` and serves it with `axum::serve(listener, app.into_make_service())`. `src/app.rs` has one route, a `HomePage` with a counter button and no server function yet. `rust-toolchain.toml` pins nightly, and the template still carries `{{project-name}}` placeholders that `cargo leptos new` fills in; `end2end/` holds a Playwright 1.44 suite.

The limiter is `tower_governor` 0.8.0, which depends on `axum` 0.8, `governor` 0.10 and `tower` 0.5, the versions already in the tree. The agent builds the config with `GovernorConfigBuilder::default().per_second(2).burst_size(10).use_headers().finish()`, keyed by client address, and applies `GovernorLayer::new(config)` to the server-function path only, the `/api` prefix where Leptos mounts every `#[server]` function, so a page load that fetches the HTML, the stylesheet, the JavaScript glue and the wasm bundle from `/pkg` never counts against the budget. A request past the budget gets `429 Too Many Requests` with `retry-after` and `x-ratelimit-after`; `use_headers` adds `x-ratelimit-limit` and `x-ratelimit-remaining` to every answer so a client can slow down before it is refused.

Two details decide whether the limiter sees a real address. The default `PeerIpKeyExtractor` reads the peer address from Axum's `ConnectInfo`, which exists only when the server is started with `into_make_service_with_connect_info::<SocketAddr>()`, so `main.rs` changes from `into_make_service()`; without that every request would fail to extract a key and answer `500`. Behind a reverse proxy the peer is the proxy, so the agent makes the extractor a one-line choice between `PeerIpKeyExtractor` and `SmartIpKeyExtractor`, which reads `x-forwarded-for`, `x-real-ip` and `forwarded` in that order, selected by a `LEPTOS_TRUST_PROXY` environment variable read next to `get_configuration`. The governor keeps one bucket per key in memory, so a `tokio::spawn` loop calls `limiter().retain_recent()` every 60 seconds to drop idle clients.

To test it the agent adds a `save_count` server function the counter button calls, which gives `/api` a real route, then an integration test under `tests/` with the `ssr` feature that builds the router and sends eleven `POST /api/save_count` requests through `tower::ServiceExt::oneshot`: ten answer `200`, the eleventh `429` with a `retry-after` header, and a `GET /` after that still answers `200` because pages are outside the layer. In its sandbox it installs the nightly toolchain, the `wasm32-unknown-unknown` target and `cargo-leptos`, replaces the `{{project-name}}` placeholders so the crate compiles, and runs `cargo leptos build`, `cargo test --features ssr` and `cargo leptos end-to-end` so the Playwright test that clicks the button still passes.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add rate limiting to this Leptos app's Axum server. Read Cargo.toml including the package.metadata.leptos block, src/main.rs, src/app.rs, src/lib.rs, rust-toolchain.toml, the end2end directory and the README first. Use tower_governor at the version compatible with the axum and tower versions in the lockfile. Apply a GovernorLayer with a per-second rate and a burst size, with use_headers, to the /api prefix where server functions are served, leaving pages and the /pkg assets unlimited. Switch the server to into_make_service_with_connect_info so the peer address is available, and make the key extractor configurable between the peer address and SmartIpKeyExtractor through an environment variable for deployments behind a proxy. Spawn a task that calls retain_recent on the limiter every minute. Add a save_count server function the counter calls so there is a real /api route, and an integration test that sends burst plus one requests and asserts 429 with retry-after on the last one while GET / still answers 200. Install nightly, the wasm target and cargo-leptos, fill in the template placeholders, run cargo leptos build, cargo test with the ssr feature and the Playwright suite, and open a pull request that quotes the output.",
  "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

The pull request changes `Cargo.toml` (`tower_governor` and `tower` as `ssr`-only dependencies), `src/main.rs` (the config, the layer on `/api`, the connect-info service and the cleanup task), `src/app.rs` (the `save_count` server function and the button that calls it) and adds `tests/rate_limit.rs`. Its description quotes the `cargo test` output with the new test, the `cargo leptos build` result and the Playwright summary, and gives the two numbers a reviewer will want to change first, the rate and the burst, with the line each lives on.

A reviewer checks the extractor choice against their deployment: a server exposed directly keeps the peer address, one behind a load balancer sets the proxy variable, and one that trusts `x-forwarded-for` from the open internet has no limiter at all, which the pull request spells out. They also confirm the layer is on `/api` only, so a slow client loading the wasm bundle is never refused.

Hydration, routing and the `/pkg` assets are unchanged, and a request under the budget carries two extra headers and nothing else.

## Run it on every Linear issue labelled rate-limit

In the console, add an automation on the Project with a `linear` trigger on `linear.issue.labeled` and the label `rate-limit`, with the prompt above. When a person on the team labels an issue that names a server function or a budget, the run starts with the issue as its context, makes the change in the Project's repository and opens a pull request on its own branch linked to the issue. A label applied by an app integration never fires it.

From your own tooling, send this body to `POST /o/{org}/sessions` with the issue text appended to the prompt and a `request_id` that is a UUID v5 of the issue id, so a retried webhook from your side returns the run already started.

## Why run it on Rush?

A Leptos project needs nightly Rust, the wasm target, `cargo-leptos`, dart-sass and a Playwright install before a single test runs, and the template's placeholders mean a fresh clone does not compile at all. The agent's sandbox sets all of that up, runs the server build, the unit test and the browser test in order, and keeps each command's output in the run's record beside the pull request.

Rate limiting is also only proven by being hit. The integration test sends the burst plus one and reads the `429` and its `retry-after`, and the run's record shows that request and that answer, which is more than a diff can show.

## Frequently asked questions

### Why limit only /api and not the whole app?

A page load in Leptos is several requests: the document, the stylesheet, the JavaScript glue and the wasm bundle under `/pkg`. Counting those would spend a client's budget on rendering the page. Server functions are the calls that do work, so the layer sits on their prefix and everything else stays open.

### What happens behind a proxy?

The peer address is the proxy's, so every user would share one bucket. Setting the variable the agent adds switches to `SmartIpKeyExtractor`, which reads `x-forwarded-for`, `x-real-ip` or `forwarded`, and the pull request warns that this must only be enabled where the proxy sets those headers itself.

### Does it work with cargo leptos watch?

Yes. The layer is part of the Axum router the `ssr` binary builds, and `cargo leptos watch` rebuilds that binary like any other change. The limiter's state lives in the server process, so a restart empties the buckets, which is the usual behaviour for an in-memory limiter.

### Why switch to into_make_service_with_connect_info?

The default key extractor reads the peer address from Axum's `ConnectInfo` extension, which only exists when the server is started that way. Without it the extractor finds no key and the middleware answers `500` on every request, so the change is required rather than a preference.

### Can different server functions have different budgets?

Yes, by nesting a router per group of functions with its own `GovernorLayer`, or by writing a key extractor that includes the path. The pull request uses one budget for all of `/api` because the app has one server function, and names the two places to split it later.

## More Leptos jobs

- [Add Request ID Logging to a Leptos App on Axum](https://getrush.ai/run/add-request-id-logging/leptos/)
- [Add Security Headers to a Leptos Axum App](https://getrush.ai/run/add-security-headers/leptos/)
- [Add Structured Logging to a Leptos Axum App](https://getrush.ai/run/add-structured-logging/leptos/)

## The same job on other stacks

- [Add Rate Limiting to a Litestar API](https://getrush.ai/run/add-rate-limiting/litestar/)
- [Add Rate Limiting to a Medusa Store API](https://getrush.ai/run/add-rate-limiting/medusa/)
- [Add Rate Limiting to a NestJS API](https://getrush.ai/run/add-rate-limiting/nestjs/)

## 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/)
