Skip to content

Commit

Permalink
Use tower-http's TimeoutLayer (#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Sep 17, 2023
1 parent 9eb502c commit a9822ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
16 changes: 6 additions & 10 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,15 +1247,14 @@ const _: () = {
#[cfg(test)]
mod tests {
use super::*;
use crate::{
body::Body, error_handling::HandleErrorLayer, extract::State,
handler::HandlerWithoutStateExt,
};
use crate::{body::Body, extract::State, handler::HandlerWithoutStateExt};
use axum_core::response::IntoResponse;
use http::{header::ALLOW, HeaderMap};
use std::time::Duration;
use tower::{timeout::TimeoutLayer, Service, ServiceExt};
use tower_http::{services::fs::ServeDir, validate_request::ValidateRequestHeaderLayer};
use tower::{Service, ServiceExt};
use tower_http::{
services::fs::ServeDir, timeout::TimeoutLayer, validate_request::ValidateRequestHeaderLayer,
};

#[crate::test]
async fn method_not_allowed_by_default() {
Expand Down Expand Up @@ -1354,10 +1353,7 @@ mod tests {
.merge(delete_service(ServeDir::new(".")))
.fallback(|| async { StatusCode::NOT_FOUND })
.put(ok)
.layer((
HandleErrorLayer::new(|_| async { StatusCode::REQUEST_TIMEOUT }),
TimeoutLayer::new(Duration::from_secs(10)),
)),
.layer(TimeoutLayer::new(Duration::from_secs(10))),
);

let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await.unwrap();
Expand Down
10 changes: 4 additions & 6 deletions axum/src/routing/tests/merge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::*;
use crate::{error_handling::HandleErrorLayer, extract::OriginalUri, response::IntoResponse, Json};
use crate::{extract::OriginalUri, response::IntoResponse, Json};
use serde_json::{json, Value};
use tower::{limit::ConcurrencyLimitLayer, timeout::TimeoutLayer};
use tower::limit::ConcurrencyLimitLayer;
use tower_http::timeout::TimeoutLayer;

#[crate::test]
async fn basic() {
Expand Down Expand Up @@ -127,10 +128,7 @@ async fn layer_and_handle_error() {
let one = Router::new().route("/foo", get(|| async {}));
let two = Router::new()
.route("/timeout", get(std::future::pending::<()>))
.layer((
HandleErrorLayer::new(|_| async { StatusCode::REQUEST_TIMEOUT }),
TimeoutLayer::new(Duration::from_millis(10)),
));
.layer(TimeoutLayer::new(Duration::from_millis(10)));
let app = one.merge(two);

let client = TestClient::new(app);
Expand Down
12 changes: 6 additions & 6 deletions axum/src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use tower::{service_fn, timeout::TimeoutLayer, util::MapResponseLayer, ServiceExt};
use tower_http::{limit::RequestBodyLimitLayer, validate_request::ValidateRequestHeaderLayer};
use tower::{service_fn, util::MapResponseLayer, ServiceExt};
use tower_http::{
limit::RequestBodyLimitLayer, timeout::TimeoutLayer,
validate_request::ValidateRequestHeaderLayer,
};
use tower_service::Service;

mod fallback;
Expand Down Expand Up @@ -301,10 +304,7 @@ async fn wildcard_sees_whole_url() {
async fn middleware_applies_to_routes_above() {
let app = Router::new()
.route("/one", get(std::future::pending::<()>))
.layer((
HandleErrorLayer::new(|_: BoxError| async move { StatusCode::REQUEST_TIMEOUT }),
TimeoutLayer::new(Duration::new(0, 0)),
))
.layer(TimeoutLayer::new(Duration::ZERO))
.route("/two", get(|| async {}));

let client = TestClient::new(app);
Expand Down

0 comments on commit a9822ec

Please sign in to comment.