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 RequestBodyTimeout and ResponseBodyTimeout #109

Closed
wants to merge 11 commits into from
Closed

Conversation

davidpdrsn
Copy link
Member

This adds two new middleware:

  • RequestBodyTimeout
  • ResponseBodyTimeout

They both do pretty much what it says on the tin.

enum State {
NotPolled(Duration),
SleepPending(#[pin] Sleep),
}
Copy link
Member Author

@davidpdrsn davidpdrsn Jun 16, 2021

Choose a reason for hiding this comment

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

Not sure how useful this would be in practice. I'm just wondering if users might receive a request and not poll the body immediately and hit the timeout because they were slow to call the first poll.

Copy link
Member

Choose a reason for hiding this comment

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

would it make sense to have a Lazy and non lazy version?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's common to first hit the DB for an auth check with a value from an authorization header before starting to look at the http body. This seems like a good idea to me.

(based on my understanding that the body would usually be polled immediately, except for use cases where the body is streamed)

Co-authored-by: Daiki Mizukami <tesaguriguma@gmail.com>
Comment on lines +3 to +4
//! Note these middleware differ from [`tower::timeout::Timeout`] which only
//! adds a timeout to the response future and doesn't consider request bodies.
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does this mean for ResponseBodyTimeout? Is it equivalent to the tower middleware?

enum State {
NotPolled(Duration),
SleepPending(#[pin] Sleep),
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's common to first hit the DB for an auth check with a value from an authorization header before starting to look at the http body. This seems like a good idea to me.

(based on my understanding that the body would usually be polled immediately, except for use cases where the body is streamed)

}
}

impl<B> Default for TimeoutBody<B>
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this impl useful for?

@@ -217,13 +218,16 @@
clippy::match_like_matches_macro,
clippy::type_complexity
)]
#![forbid(unsafe_code)]
#![cfg_attr(not(test), forbid(unsafe_code))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like all unsafe usage went away in later commits, so this can be reverted?

@davidpdrsn davidpdrsn added this to the 0.2.0 milestone Nov 8, 2021
@davidpdrsn
Copy link
Member Author

I've been thinking that we probably shouldn't add this to tower-http quite yet. I think it would be better for something like this to be built and mature in a project somewhere and then moved into tower-http later. So I'll close this PR for now.

Let me know if you feel this is important.

@davidpdrsn davidpdrsn closed this Nov 15, 2021
@davidpdrsn davidpdrsn deleted the body-timeout branch November 15, 2021 11:02
@LucioFranco
Copy link
Member

@davidpdrsn we should add this stuff into tonic and let it mature there maybe? Its quite an important feature for running code in prod.

@davidpdrsn
Copy link
Member Author

Sure we could do that!

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.

None yet

4 participants