You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use axum::{routing::get,Router};use tower::limit::ConcurrencyLimitLayer;asyncfnhandler(){}let app = Router::new().route("/",// All requests to `GET /` will be sent through `ConcurrencyLimitLayer`get(handler).layer(ConcurrencyLimitLayer::new(64)),);
This compiles, but no concurrency limit is enforced. This is because each request to GET / is sent through a different ConcurrencyLimit instance, each with its own semaphore.
Full runnable example (test with hey -n 50 http://localhost:3000/api/test):
use axum::{routing::get,Router};use std::time::Duration;use tower::limit::ConcurrencyLimitLayer;asyncfnhandler(){eprintln!("/api/test in");
tokio::time::sleep(Duration::from_secs(1)).await;eprintln!("/api/test out");}#[tokio::main]asyncfnmain(){let app = Router::new().route("/api/test",get(handler).layer(ConcurrencyLimitLayer::new(5)),);// run our app with hyper, listening globally on port 3000let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();}
The text was updated successfully, but these errors were encountered:
Oh thanks for noticing! I'm actually surprised its not working. I would have expected #2483 to fix it since we no longer clone the router for each request, which we used to in 0.7 until 07.4.
I did some testing and adding .with_state(()); at the end of the router fixes it:
#[tokio::main]asyncfnmain(){let app = Router::new().route("/api/test",get(handler).layer(ConcurrencyLimitLayer::new(2)),).with_state(());// <-- ADD THIS// run our app with hyper, listening globally on port 3000let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();}
I'm not totally sure why that is but I'll do some more digging.
The docs for
MethodRouter::layer
contain this example:This compiles, but no concurrency limit is enforced. This is because each request to
GET /
is sent through a differentConcurrencyLimit
instance, each with its own semaphore.Full runnable example (test with
hey -n 50 http://localhost:3000/api/test
):The text was updated successfully, but these errors were encountered: