-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
handle_error
increases compiles a lot
#145
Comments
On my machine the difference is even worse. Around 10s for edit(1): Same with rustc 1.56.0-nightly (a6ece5615 2021-08-03) |
Ran with
|
Yeah, in |
On my machine (macos m1), using handle_error took 10s (1/3) longer than without handle_error. |
That's not going to help. Usage:
|
I just filed rust-lang/rust#87924 |
I've managed to find a work around that doesn't impact compile times: use axum::body::{box_body, BoxBody};
use axum::prelude::*;
use futures::future::{FutureExt, Map};
use http::Response;
use std::task::{Context, Poll};
use std::{net::SocketAddr, time::Duration};
use tower::Service;
use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;
#[tokio::main]
async fn main() {
// Set the RUST_LOG, if it hasn't been explicitly defined
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "hello_world=debug")
}
tracing_subscriber::fmt::init();
let app = route("/", get(handler))
.nest(
"/api",
route("/pay/get_pay_params", post(handler))
.nest(
"/user",
route("/create", post(handler))
.route("/login", post(handler))
.route("/info", get(handler))
.route("/update_password", get(handler)),
)
.nest(
"/product",
route("/list", get(handler)).route("/detail", get(handler)),
),
)
.layer(
ServiceBuilder::new()
.timeout(Duration::from_secs(10))
.layer(TraceLayer::new_for_http())
.into_inner(),
);
let app = app.layer(tower::layer::layer_fn(HandleError::new));
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn handler() -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
}
#[derive(Debug, Clone)]
pub struct HandleError<S> {
inner: S,
}
impl<S> HandleError<S> {
pub fn new(inner: S) -> Self {
Self { inner }
}
}
impl<S, R, B> Service<R> for HandleError<S>
where
S: Service<R, Response = Response<B>>,
S::Error: Into<BoxError>,
B: axum::body::HttpBody<Data = axum::body::Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError>,
{
type Response = Response<BoxBody>;
type Error = S::Error;
#[allow(clippy::type_complexity)]
type Future =
Map<S::Future, fn(Result<S::Response, Self::Error>) -> Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: R) -> Self::Future {
self.inner.call(req).map(|result| match result {
Ok(res) => Ok(res.map(box_body)),
Err(err) => Ok(handle_error(err.into())),
})
}
}
fn handle_error(error: BoxError) -> Response<BoxBody> {
todo!("your code here")
} The only downside is that it doesn't change the error type of the service to |
I just merged #184 which should help compile times a bit. Let me know if someone tries it! |
I got a very small improvement. Before your change, my project cargo check took ~60 seconds. With your commit takes 44.83s. If I add more routes rustc stays running until reaches OOM. (I cloned axum git repo to my disk and changed in the cargo.toml to read it, to discard any cargo cache) |
@vaniusrb Can you share a reproduction? |
This brings the compile time of the example posted [here][example] from 3 seconds down to 0.3 seconds for me. Having the bounds on the methods does improve UX but not worth sacrificing 10x compile time for. [example]: #145 (comment)
@davidpdrsn Here: https://github.com/vaniusrb/axum-slow-compile |
* Improve compile times of `handle_error` This brings the compile time of the example posted [here][example] from 3 seconds down to 0.3 seconds for me. Having the bounds on the methods does improve UX but not worth sacrificing 10x compile time for. [example]: #145 (comment) * Improve compile time of `check_infallible` * update changelog
@davidpdrsn Confirmed! Thank you very much. You are awesome. |
If someone still has compile time issues when using |
It seems adding
handle_error
to a router will increase compile times quite significantly.Here is an example:
With the
handle_error
it compiles in 5s and in 0.3s without, on my machine in debug mode. Whether this is a rustc issue or something we can improve directly via axum I don't know. Something to investigate for sure.The text was updated successfully, but these errors were encountered: