Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tower-http's TimeoutLayer #2231

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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