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

Add Timeout middleware #270

Merged
merged 3 commits into from
May 18, 2022
Merged

Add Timeout middleware #270

merged 3 commits into from
May 18, 2022

Conversation

davidpdrsn
Copy link
Member

tower::timeout::Timeout doesn't work well with axum since it changes the error type to BoxError. That requires doing this:

let app = Router::new()
    .route("/", get(|| async {}))
    .layer(
        ServiceBuilder::new()
            .layer(HandleErrorLayer::new(handle_timeout_error))
            .timeout(Duration::from_secs(30))
    );

async fn handle_timeout_error(err: BoxError) -> (StatusCode, String) {
    if err.is::<tower::timeout::error::Elapsed>() {
        (
            StatusCode::REQUEST_TIMEOUT,
            "Request took too long".to_string(),
        )
    } else {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Unhandled internal error: {}", err),
        )
    }
}

Thats quite a mouthful. With this middleware that becomes:

let app = Router::new()
    .route("/", get(|| async {}))
    .layer(TimeoutLayer::new(Duration::from_secs(30));

This works because it doesn't change the service's error type and thus keeps Infallible which axum requires.

@davidpdrsn davidpdrsn added the A-new-middleware Area: new middleware proposals label May 17, 2022
Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except one somewhat clunky sentence in the docs.

tower-http/src/timeout.rs Outdated Show resolved Hide resolved
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@davidpdrsn davidpdrsn merged commit 960c83b into master May 18, 2022
@davidpdrsn davidpdrsn deleted the timeout branch May 18, 2022 18:58
@LucioFranco
Copy link
Member

I wonder if this should just either 1) wrap the main tower timeout and just map the error (with a way to customize it, 2) we should just make tower timeout more flexible.

Also, this rases a question can you use other regular tower services or are you limited to ones just in tower-http because of the error shape?

@davidpdrsn
Copy link
Member Author

I wonder if this should just either 1) wrap the main tower timeout and just map the error (with a way to customize it, 2) we should just make tower timeout more flexible.

I think its okay for tower-http to provide its own without wrapping tower's, since timeouts are so simple. For more complex middleware it might be better to wrap tower's.

Also, this rases a question can you use other regular tower services or are you limited to ones just in tower-http because of the error shape?

For axum there are no limits thanks to HandleErrorLayer and MethodRouter::handle_error so its mostly a question of ergonomics.

I haven't don't often use other middleware from tower (except timeout) that changes the error type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-new-middleware Area: new middleware proposals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants