Use tower-http's TimeoutLayer (#2231)

This commit is contained in:
David Pedersen
2023-09-17 18:02:22 +02:00
committed by GitHub
parent 9eb502c768
commit a9822ec80b
3 changed files with 16 additions and 22 deletions
+6 -10
View File
@@ -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() {
@@ -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();
+4 -6
View File
@@ -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() {
@@ -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);
+6 -6
View File
@@ -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;
@@ -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);