How to set timeouts? #1383
-
I'm coming from Golang where I can simply use func start() {
httpServer := &http.Server{
ReadTimeout: "30s",
WriteTimeout: "1h",
//...
}
err = httpServer.ListenAndServe()
} I'm using Can you help me? I know I'm using the below code: axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap(); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
You can use https://docs.rs/tower-http/latest/tower_http/timeout/index.html |
Beta Was this translation helpful? Give feedback.
-
I tried this but for some reason my handler keeps getting called over and over again without hitting a timeout, eventually failing with a connection reset. Router: pub async fn timeout(method: Method, uri: Uri, err: BoxError) -> impl IntoResponse {
if err.is::<Elapsed>() {
(
StatusCode::REQUEST_TIMEOUT,
format!("request timeout: {method} on {uri}"),
)
} else {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("internal server error: {method} on {uri}"),
)
}
} let timeout_layer = ServiceBuilder::new()
.layer(HandleErrorLayer::new(handlers::timeout))
.timeout(Duration::from_secs(1));
Router::new()
.merge(SpaRouter::new("/tmp", ".tmp"))
.route("/", get(handlers::get_root))
...
.layer(timeout_layer.into_inner())
.route_layer(middleware::from_fn(signed_in))
.layer(session_layer)
.with_state(app)
.layer(TraceLayer::new_for_http())
.layer(DefaultBodyLimit::disable()) I replicated the exact same steps. |
Beta Was this translation helpful? Give feedback.
You can use https://docs.rs/tower-http/latest/tower_http/timeout/index.html