diff --git a/axum-examples/src/main.rs b/axum-examples/src/main.rs index 4f683d9..97e4bd7 100644 --- a/axum-examples/src/main.rs +++ b/axum-examples/src/main.rs @@ -27,7 +27,7 @@ async fn main() { "/", get(|| async { // simulate a long request - tokio::time::sleep(Duration::from_secs(29)).await; + tokio::time::sleep(Duration::from_secs(11)).await; "Hello, World!" }), ) @@ -36,7 +36,7 @@ async fn main() { // `timeout` will produce an error if the handler takes // too long so we must handle those .layer(HandleErrorLayer::new(handle_timeout_error)) - .timeout(Duration::from_secs(30)), + .timeout(Duration::from_secs(10)), ); axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) diff --git a/axum-like-examples/src/main.rs b/axum-like-examples/src/main.rs index 8dc3a58..7c345d7 100644 --- a/axum-like-examples/src/main.rs +++ b/axum-like-examples/src/main.rs @@ -3,6 +3,8 @@ use http::{HeaderValue, StatusCode}; use std::convert::Infallible; use std::future::Future; use std::net::SocketAddr; +use std::thread::Thread; +use std::time::Duration; use axum_like::extract::{Body, Query, TypedHeader}; use axum_like::handler::put; @@ -19,15 +21,15 @@ async fn main() { .layer(SetRequestHeaderLayer::<_, Body>::overriding( USER_AGENT, HeaderValue::from_static("axum-like demo"), - )); - - // handler error - let app = app.handle_error(|error: Infallible| { - Ok::<_, Infallible>(( - StatusCode::INTERNAL_SERVER_ERROR, - "Unhandled internal error".to_string(), )) - }); + .layer(TimeoutLayer::new(Duration::from_secs(10))) + .handle_error(|error: BoxError| { + println!("Unhandled internal error: {}", error); + Ok::<_, Infallible>(( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Unhandled internal 111 error: {}", error), + )) + }); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -66,6 +68,8 @@ async fn post_handler() -> &'static str { // let s = "sss"; // let i = s.parse::().unwrap(); // println!("i: {}", i); + //Thread::sleep(Duration::from_secs(11)); + tokio::time::sleep(Duration::from_secs(11)).await; "

Post Hello, World!

" } @@ -74,6 +78,8 @@ async fn put_handler() -> &'static str { } use serde::Deserialize; +use tower::timeout::TimeoutLayer; +use tower::Layer; use tower_http::set_header::SetRequestHeaderLayer; #[derive(Deserialize, Debug)] diff --git a/axum-like/img/handler_error.jpeg b/axum-like/img/handler_error.jpeg new file mode 100644 index 0000000..447d002 Binary files /dev/null and b/axum-like/img/handler_error.jpeg differ diff --git a/axum-like/readme.md b/axum-like/readme.md index 19d1253..6b1fe15 100644 --- a/axum-like/readme.md +++ b/axum-like/readme.md @@ -63,6 +63,9 @@ pub trait Service { ![画板](img/extra.jpeg) +## handler_error 时序图 + +![画板](img/handler_error.jpeg) https://github.com/davidpdrsn/axum-handle-error-slow-compile diff --git a/axum-like/src/handler.rs b/axum-like/src/handler.rs index 82ca1f9..0a04b41 100644 --- a/axum-like/src/handler.rs +++ b/axum-like/src/handler.rs @@ -355,6 +355,7 @@ where } fn call(&mut self, req: Request) -> Self::Future { + println!("HandleError call"); future::HandleErrorFuture { f: Some(self.f.clone()), inner: self.inner.clone().oneshot(req), diff --git a/axum-like/src/lib.rs b/axum-like/src/lib.rs index 8f84eeb..3fac135 100644 --- a/axum-like/src/lib.rs +++ b/axum-like/src/lib.rs @@ -14,7 +14,6 @@ pub mod extract; pub mod handler; pub mod response; mod util; - pub use self::router::Router; /// Alias for a type-erased error type.