Middleware, static files and HTTP caching
id_effect_axum ships the middleware a production HTTP service needs as tower layers with
working defaults: CompressionLayer, RequestIdLayer, RateLimitLayer, BodyLimitLayer and
TimeoutLayer. It also serves static files (serve_dir) with conditional GET, and caches
dynamic responses with HttpCacheLayer. Every rejection is an RFC 9457 Problem
(application/problem+json).
| Need | API | Default | Rejection |
|---|---|---|---|
| Compress responses | middleware::CompressionLayer | gzip, brotli, deflate for bodies of 860 bytes or more | — |
| Correlate requests | middleware::RequestIdLayer, extractor RequestId | keeps a valid incoming x-request-id, else generates one | — |
| Limit request rate | middleware::RateLimitLayer | token bucket per client IP | 429 + Retry-After |
| Limit body size | middleware::BodyLimitLayer | 2 MiB | 413 |
| Bound request time | middleware::TimeoutLayer | 30 s, the handler's fiber is interrupted | 503 |
| Cache dynamic responses | middleware::HttpCacheLayer, CacheControl | private, no-cache and a body-hash ETag | 304 |
| Serve files | server::serve_dir | ETag, Last-Modified, precompressed .br / .gz | 304, 404, 412 |
The runnable version of this chapter is
crates/id_effect_axum/examples/090_middleware.rs:
an API and a static site behind the whole stack.
cargo run -p id_effect_axum --example 090_middleware # sends a few requests, prints them
cargo run -p id_effect_axum --example 090_middleware -- --serve # listens on 127.0.0.1:3000
Stacking the layers
Stack the layers with Router::layer. The last call is the outermost layer. Put
RequestIdLayer outermost so every response carries x-request-id, rejections included. Then
come the rate limit, the body limit, the timeout and compression:
#![allow(unused)] fn main() { use std::convert::Infallible; use std::net::SocketAddr; use std::time::Duration; use axum::Router; use axum::body::Body; use axum::extract::ConnectInfo; use axum::http::{Request, StatusCode}; use id_effect::{Effect, succeed}; use id_effect_axum::middleware::{ BodyLimitLayer, CompressionLayer, REQUEST_ID_HEADER, RateLimitLayer, RequestId, RequestIdLayer, TimeoutLayer, }; use id_effect_axum::routing; use tower::ServiceExt; fn hello(_: &mut ()) -> Effect<String, Infallible, ()> { succeed("hello".to_owned()) } let app: Router = Router::new() .route("/hello", routing::get(hello)) .route( "/whoami", axum::routing::get(|id: RequestId| async move { format!("request {id}") }), ) .layer(CompressionLayer::new()) .layer(TimeoutLayer::new(Duration::from_secs(10))) .layer(BodyLimitLayer::new(64 * 1024)) .layer(RateLimitLayer::per_second(50.0, 100.0)) .layer(RequestIdLayer::new()); tokio::runtime::Runtime::new().unwrap().block_on(async { let mut req = Request::builder() .uri("/whoami") .header(REQUEST_ID_HEADER, "demo-1") .body(Body::empty()) .unwrap(); // What `into_make_service_with_connect_info::<SocketAddr>()` adds when serving. req.extensions_mut().insert(ConnectInfo(SocketAddr::from(([127, 0, 0, 1], 4000)))); let res = app.oneshot(req).await.unwrap(); assert_eq!(res.status(), StatusCode::OK); assert_eq!(res.headers()[REQUEST_ID_HEADER], "demo-1"); }); }
The request id reaches handlers as the RequestId extractor. Each request runs inside an
http.request effect span with the method, the route template, the id and the status. The span
goes to the tracing buffer or, with .span_hook(..), to OpenTelemetry.
Timeouts interrupt the handler
TimeoutLayer does not just stop waiting. It interrupts the handler's fiber, so its ensuring
finalizers run and its scoped resources are released. No work outlives the 503:
#![allow(unused)] fn main() { use std::convert::Infallible; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use axum::Router; use axum::body::Body; use axum::http::{Request, StatusCode}; use id_effect::{sleep, sync}; use id_effect_axum::middleware::TimeoutLayer; use id_effect_axum::{PROBLEM_JSON, routing}; use tower::ServiceExt; let released = Arc::new(AtomicBool::new(false)); let flag = released.clone(); let app: Router = Router::new() .route( "/slow", routing::get(move |_: &mut ()| { let flag = flag.clone(); sleep::<Infallible, ()>(Duration::from_secs(30)) .map(|()| "done") .ensuring(sync(move || flag.store(true, Ordering::SeqCst))) }), ) .layer(TimeoutLayer::new(Duration::from_millis(50))); tokio::runtime::Runtime::new().unwrap().block_on(async { let req = Request::builder().uri("/slow").body(Body::empty()).unwrap(); let res = app.oneshot(req).await.unwrap(); assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE); assert_eq!(res.headers()["content-type"], PROBLEM_JSON); while !released.load(Ordering::SeqCst) { tokio::time::sleep(Duration::from_millis(5)).await; } }); }
Rate limiting
RateLimitLayer::per_second(rate, burst) limits each client IP. The IP comes from
into_make_service_with_connect_info::<SocketAddr>(); without it, all requests share one
bucket. To key by principal, use RateLimitKey::from_fn(..).or(RateLimitKey::peer_ip()). For
per-tenant limits, RateLimitKey::tenant(|p: &T| ..) keys by the tenant of a value T the
authentication layer recorded (a RequestCaps override, else a request extension) — e.g.
RateLimitKey::tenant(|p: &CurrentPrincipal| p.tenant().map(str::to_owned)) — so every user of
an organisation shares its bucket; add the rate limit layer inside the authentication layer
(Tenancy and the audit log has the whole example). Other keys:
RateLimitKey::header(name) (an API key) and RateLimitKey::global(). To share buckets across
instances, implement your own RateLimitStore (or use id_effect_redis's
RedisRateLimitStore). A store's acquire is an effect, so it can do I/O:
#![allow(unused)] fn main() { use std::sync::atomic::{AtomicU32, Ordering}; use axum::{Router, routing::get}; use id_effect::{Effect, HNil, fail, succeed}; use id_effect_axum::middleware::{RateLimitError, RateLimitKey, RateLimitLayer, RateLimitStore}; /// A stand-in for a shared store (e.g. Redis `INCR` with a TTL): 100 requests in total. #[derive(Default)] struct SharedStore { used: AtomicU32, } impl RateLimitStore for SharedStore { fn acquire(&self, _key: &str) -> Effect<(), RateLimitError, HNil> { if self.used.fetch_add(1, Ordering::SeqCst) < 100 { succeed(()) } else { fail(RateLimitError) } } } #[derive(Clone)] struct Principal(String); let by_user = RateLimitKey::from_fn(|req| { req.extensions().get::<Principal>().map(|p| format!("user:{}", p.0)) }); let app: Router = Router::new() .route("/api", get(|| async { "ok" })) .layer(RateLimitLayer::new(SharedStore::default()).key(by_user.or(RateLimitKey::peer_ip()))); let _ = app; }
Static files
server::serve_dir(root) serves a directory as a tower Service. Mount it with
Router::nest_service or as the router's fallback_service. It is built on tower-http's
ServeDir and adds the parts a cache needs:
- Validators. Every file has
Last-Modifiedand a strongETagbuilt from its modification time, size and encoding. A matchingIf-None-Matchgets304 Not Modified, and so does anIf-Modified-Sincethat is not older than the file. When both headers are present,If-None-Matchdecides.If-MatchandIf-Unmodified-Sinceanswer412.If-Rangedecides whether aRangerequest is honoured. - Precompressed variants. If the client accepts
brorgzipandapp.js.brorapp.js.gzexists next toapp.js, that file is sent withContent-Encodingset. Every response carriesVary: Accept-Encoding. - Cache-Control. Content-hashed names such as
app.3f2a9c1b.jsgetpublic, max-age=31536000, immutable. Everything else, HTML included, getsno-cache, so the browser revalidates it with theETagon each use. To use one policy for every file, call.cache_control(CacheControl::..). - Safe paths. Requests cannot leave the root through
..segments or absolute paths. Hidden files (.env,.git/, percent-encoded or not) are never served;.well-knownis the one exception. A miss is a404Problem.
#![allow(unused)] fn main() { use axum::Router; use axum::body::Body; use axum::http::{Request, StatusCode, header}; use id_effect_axum::server::serve_dir; use tower::ServiceExt; let public = std::env::temp_dir().join(format!("book_ch29_03_{}", std::process::id())); std::fs::create_dir_all(&public).unwrap(); std::fs::write(public.join("index.html"), "<h1>hello</h1>").unwrap(); std::fs::write(public.join("app.3f2a9c1b.js"), "console.log(1)").unwrap(); std::fs::write(public.join(".env"), "SECRET=1").unwrap(); let app: Router = Router::new().fallback_service(serve_dir(&public)); tokio::runtime::Runtime::new().unwrap().block_on(async { let get = |uri: &str| Request::builder().uri(uri).body(Body::empty()).unwrap(); let page = app.clone().oneshot(get("/")).await.unwrap(); assert_eq!(page.status(), StatusCode::OK); assert_eq!(page.headers()[header::CACHE_CONTROL], "no-cache"); let etag = page.headers()[header::ETAG].clone(); // The browser revalidates: same file, no body. let mut revalidate = get("/"); revalidate.headers_mut().insert(header::IF_NONE_MATCH, etag); let res = app.clone().oneshot(revalidate).await.unwrap(); assert_eq!(res.status(), StatusCode::NOT_MODIFIED); let script = app.clone().oneshot(get("/app.3f2a9c1b.js")).await.unwrap(); assert_eq!(script.headers()[header::CACHE_CONTROL], "public, max-age=31536000, immutable"); for hidden in ["/.env", "/%2eenv", "/../etc/passwd"] { let res = app.clone().oneshot(get(hidden)).await.unwrap(); assert_eq!(res.status(), StatusCode::NOT_FOUND); } }); std::fs::remove_dir_all(&public).unwrap(); }
Caching dynamic responses
HttpCacheLayer does the same for handlers. For a 200 answer to GET it:
- sets its
Cache-Control, unless the handler set one; - keeps a handler's own
ETag, or computes a strong one from a SHA-256 hash of the body. Only bodies up tobody_limit(1 MiB by default) are hashed; larger or streaming bodies pass through without anETag; - answers a matching
If-None-Matchwith304. If the request has noIf-None-Match, it comparesIf-Modified-Sincewith the handler'sLast-Modifiedinstead.
The handler still runs for a conditional request. The layer saves bandwidth, not work. Responses
marked no-store are left alone. Put the layer outside CompressionLayer, so the ETag is
the hash of the bytes actually sent and each encoding gets its own tag. If the layer must sit
inside compression, use .weak_etags().
#![allow(unused)] fn main() { use std::convert::Infallible; use std::time::Duration; use axum::Router; use axum::body::Body; use axum::http::{Request, StatusCode, header}; use id_effect::{Effect, succeed}; use id_effect_axum::middleware::{CacheControl, CompressionLayer, HttpCacheLayer}; use id_effect_axum::routing; use tower::ServiceExt; fn catalog(_: &mut ()) -> Effect<&'static str, Infallible, ()> { succeed(r#"[{"sku":"tea"}]"#) } let app: Router = Router::new() .route("/catalog", routing::get(catalog)) .layer(CompressionLayer::new()) .layer(HttpCacheLayer::new().cache_control(CacheControl::private(Duration::from_secs(60)))); tokio::runtime::Runtime::new().unwrap().block_on(async { let get = || Request::builder().uri("/catalog").body(Body::empty()).unwrap(); let first = app.clone().oneshot(get()).await.unwrap(); assert_eq!(first.headers()[header::CACHE_CONTROL], "private, max-age=60"); let etag = first.headers()[header::ETAG].clone(); let mut again = get(); again.headers_mut().insert(header::IF_NONE_MATCH, etag); let res = app.clone().oneshot(again).await.unwrap(); assert_eq!(res.status(), StatusCode::NOT_MODIFIED); }); }
CacheControl has the common presets: no_cache(), private_no_cache() (the layer's default,
safe for per-user responses), no_store(), immutable(), public(max_age) and
private(max_age). For any other directive list, use from_static("...").
What to remember
- Layers stack with
Router::layer, and the last call is the outermost. PutRequestIdLayeroutermost. - A timeout interrupts the handler's fiber, so its finalizers run.
RateLimitLayer::per_secondneeds connect info to tell clients apart.serve_dirgives files anETagandLast-Modified, answers304, serves precompressed variants and never serves hidden files or paths outside the root.HttpCacheLayergives dynamicGETresponsesCache-Controland a body-hashETag, and answersIf-None-Matchwith304.