Skip to content

Should TimeoutLayer middleware allow returning other status codes? #300

@mlodato517

Description

@mlodato517

Feature Request

Motivation

I'd like to be able to return a 504 if the timeout given to the TimeoutLayer
is exceeded. This is because a 408, which is currently returned, is meant to
communicate
:

that the server did not receive a complete request message within the time
that it was prepared to wait.

I don't know if in general we can tell if the issue is the client sending the
request or the server processing the request but in the case of the latter
(especially because this service is proxying to another) I'd like to return
a 504 Gateway Timeout instead (which is still not a guarantee - the issue
could have been the upstream or any post-processing of the upstream's
response but that's rare in this case). Servers like axum and actix-web
provide other mechanisms for timeouts when receiving things like request
headers12 so maybe something about receiving the request should be handled
at the server level or by something like BodyTimeout?

Proposal

Changing the response status from 408 all the time to 504 all the time
would equally be incorrect sometimes and would be a breaking change.
Luckily we already have a new() constructor for TimeoutLayer so I
imagine that could stay the same and default to 408 and then we could
have a new_with_response constructor that accepts an http::Response<B>
or similar that can be returned instead.

Alternatives

The most obvious alternative I can see follows from this comment
which is to use tower instead of tower-http and explicitly catch the
error and perform custom logic on it. This isn't too much code but I
wonder if the alternate constructor is valuable because I'm not sure
how frequent 408 will be the desired response code (though HTTP
semantics are fuzzy enough that maybe it's not worth it!).

Footnotes

  1. https://docs.rs/axum-server/latest/axum_server/struct.HttpConfig.html#method.http1_header_read_timeout

  2. https://docs.rs/actix-web/latest/actix_web/struct.HttpServer.html#method.client_request_timeout

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: A PR with an enhancement or a proposed on in an issue.I-needs-decisionIssues in need of decision.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions