rust
48 lines · 7 steps
Per-IP rate limiting in Axum with tower-governor
Build an Axum router that throttles requests per client IP and prunes stale limiter state on a background loop.
Explained by
highlit
1use std::net::SocketAddr;
2use std::time::Duration;
3
4use axum::{routing::get, Json, Router};
5use serde_json::{json, Value};
6use tower_governor::governor::GovernorConfigBuilder;
7use tower_governor::key_extractor::SmartIpKeyExtractor;
8use tower_governor::GovernorLayer;
9
10pub fn api_router() -> Router {
11 let governor_conf = GovernorConfigBuilder::default()
12 .per_second(2)
13 .burst_size(5)
14 .key_extractor(SmartIpKeyExtractor)
15 .finish()
16 .expect("valid governor configuration");
17
18 let governor_conf = std::sync::Arc::new(governor_conf);
19 let limiter = governor_conf.limiter().clone();
20
21 tokio::spawn(async move {
22 let interval = Duration::from_secs(60);
23 loop {
24 tokio::time::sleep(interval).await;
25 tracing::debug!("rate limit storage size: {}", limiter.len());
26 limiter.retain_recent();
27 }
28 });
29
30 Router::new()
31 .route("/api/quotes", get(list_quotes))
32 .layer(GovernorLayer { config: governor_conf })
33}
34
35async fn list_quotes() -> Json<Value> {
36 Json(json!({
37 "quotes": [
38 { "author": "Hopper", "text": "Mind your own business is a poor motto." },
39 { "author": "Knuth", "text": "Premature optimization is the root of all evil." }
40 ]
41 }))
42}
43
44pub async fn serve(addr: SocketAddr) -> std::io::Result<()> {
45 let listener = tokio::net::TcpListener::bind(addr).await?;
46 let app = api_router().into_make_service_with_connect_info::<SocketAddr>();
47 axum::serve(listener, app).await
48}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Rate limiting is added declaratively as a tower layer wrapping your routes, keeping handlers oblivious to it.
- 2Per-IP throttling needs the connection's address, which is why the service is built with connect-info.
- 3In-memory limiter state grows unbounded unless you periodically prune entries you no longer need.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/per-ip-rate-limiting-in-axum-with-tower-governor-explained-rust-cc97/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.