-
Notifications
You must be signed in to change notification settings - Fork 341
make StreamExt::timeout(d).next() behave like future::timeout(d, s.next()) #732
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
Conversation
Eagerly waiting for this to be merged :) |
@yoshuawuyts @k-nasa @stjepang Can you please have a quick look? |
@dignifiedquire Would you mind reviewing this PR? Thanks a lot! |
I can see the use for this but not entirely sure. Pinging @yoshuawuyts for feedback |
Could you maybe share more on what this does, what the difference with the other method is, and why it would make sense to have both? |
let mut s = stream::from_iter(0..3).chain(stream::pending()).timeout(Duration::from_secs(1));
let time = Instant::now();
for _ in 0..5 {
let v = s.next().await;
println!("{}ms {:?}", time.elapsed().as_millis(), v);
}
// output:
// 0ms Some(Ok(0))
// 0ms Some(Ok(1))
// 0ms Some(Ok(2))
// 1000ms Some(Err(TimeoutError { _private: () }))
// 1000ms Some(Err(TimeoutError { _private: () }))
let mut s = stream::from_iter(0..3).chain(stream::pending()).timeout_repeat(Duration::from_secs(1));
let time = Instant::now();
for _ in 0..5 {
let v = s.next().await;
println!("{}ms {:?}", time.elapsed().as_millis(), v);
}
// output:
// 0ms Some(Ok(0))
// 0ms Some(Ok(1))
// 0ms Some(Ok(2))
// 1000ms Some(Err(TimeoutError { _private: () }))
// 2001ms Some(Err(TimeoutError { _private: () })) |
To add on what @hhggit already wrote, the default behavior of |
I see. Thanks for clarifying. I'm still confused about the motivating use case — to the best of my understanding it is: "We would like to detect timeouts, and even in the face of timeouts we would like to continue streaming." I'm not sure when this behavior would make sense. The However I don't think we should have both methods since it would appear a single method could cover both uses. |
For example, when client stops making request for sometime, a server would try to send a heartbeat to test if client is freezing or just idling. |
I'm fine with changing the default behaviour to match |
This is indeed the change I think needs to happen. |
@yoshuawuyts updated |
I feel like the new changes are how I would expect this to work, but maybe not how most people would expect this to work. If we are going this direction there may be room for something like (e.g. if you're contacting a foreign api over the network you may want to timeout even if it is sending tcp hearbeats.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good; thanks so much!
This PR is to make
s.timeout(d).next()
behave likefuture::timeout(d, s.next())
.To illustrate the difference: