Skip to content

Commit

Permalink
Merge pull request #77 from 847850277/issues_75
Browse files Browse the repository at this point in the history
[ISSUES#75] handler_error sequence diagram
  • Loading branch information
847850277 authored Nov 21, 2024
2 parents a4cf6da + 14f27a2 commit 4f30a0a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions axum-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}),
)
Expand All @@ -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())
Expand Down
22 changes: 14 additions & 8 deletions axum-like-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -66,6 +68,8 @@ async fn post_handler() -> &'static str {
// let s = "sss";
// let i = s.parse::<i32>().unwrap();
// println!("i: {}", i);
//Thread::sleep(Duration::from_secs(11));
tokio::time::sleep(Duration::from_secs(11)).await;
"<h1> Post Hello, World!</h1>"
}

Expand All @@ -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)]
Expand Down
Binary file added axum-like/img/handler_error.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions axum-like/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub trait Service<Request> {

![画板](img/extra.jpeg)

## handler_error 时序图

![画板](img/handler_error.jpeg)


https://github.com/davidpdrsn/axum-handle-error-slow-compile
Expand Down
1 change: 1 addition & 0 deletions axum-like/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ where
}

fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
println!("HandleError call");
future::HandleErrorFuture {
f: Some(self.f.clone()),
inner: self.inner.clone().oneshot(req),
Expand Down
1 change: 0 additions & 1 deletion axum-like/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4f30a0a

Please sign in to comment.