Skip to content

Add future::timeout() #20

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

Merged
merged 5 commits into from
Aug 30, 2019
Merged

Add future::timeout() #20

merged 5 commits into from
Aug 30, 2019

Conversation

ghost
Copy link

@ghost ghost commented Aug 14, 2019

cc #18

@skade
Copy link
Collaborator

skade commented Aug 14, 2019

Can we somehow mark interfaces as experimental?

@ghost
Copy link
Author

ghost commented Aug 14, 2019

We could do this:

@skade
Copy link
Collaborator

skade commented Aug 14, 2019

Sounds good! Maybe with a config flag to avoid them?

@ghost
Copy link
Author

ghost commented Aug 15, 2019

You mean feature flag?

@skade
Copy link
Collaborator

skade commented Aug 15, 2019

Ah, right - yes, a feature flag.

@ghost
Copy link
Author

ghost commented Aug 15, 2019

I feel reluctant to put stuff behind feature flags at this point since we're releasing a preview version with plenty of time till 1.0 release, and can do breaking changes before then. Wdyt?

@skade
Copy link
Collaborator

skade commented Aug 15, 2019

You are right, if we are reasonably sure, we can do it this way.

type Output = Result<F::Output, TimeoutError>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.as_mut().future().poll(cx) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it mean that in case of a CPU-bound future that is blocking the current thread you will never try to poll the timer and return TimeoutError?

Copy link
Collaborator

Choose a reason for hiding this comment

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

/cc @stjepang

Copy link
Author

Choose a reason for hiding this comment

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

@kpp That's a good point - do you think we should check for timeout before polling the future?

I worry a bit that this would add overhead to futures that are quick to poll or immediately ready. And if the future is CPU-bound and takes a really long time, we can still get stuck if the timeout occurs while that future is running.

All things considered, I think I prefer the current behavior. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

All things considered, I think I prefer the current behavior.

Yep... I don't see a better solution for this.

Still I would rewrite the current implementation as f.select(Delay::new(dur))... ;)

@skade skade assigned ghost Aug 26, 2019
@kpp
Copy link
Contributor

kpp commented Aug 26, 2019

Also notice how this code can be reduced: #113

@kpp
Copy link
Contributor

kpp commented Aug 26, 2019

BTW what's the difference between future::timeout() and io::timeout()?

@skade
Copy link
Collaborator

skade commented Aug 26, 2019

@kpp io::timeout takes the io::Error of the I/O Operation and forwards it, and itself errors as io::Error with kind TimeoutError. future::timeout does none of that and instead wraps the underlying operation. If the timed out operation returns a result, future::timeout returns Result<Result<...>>, while io::timeout only returns io::Result<...>.

Stjepan Glavina and others added 4 commits August 27, 2019 13:09
Co-Authored-By: Yoshua Wuyts <yoshuawuyts+github@gmail.com>
Co-Authored-By: Yoshua Wuyts <yoshuawuyts+github@gmail.com>
@ghost ghost force-pushed the future-timeout branch from 3643bcd to fc9091c Compare August 27, 2019 11:31
@ghost
Copy link
Author

ghost commented Aug 27, 2019

I put this function behind the new unstable feature flag.

@ghost ghost requested a review from yoshuawuyts August 28, 2019 20:10
Copy link
Contributor

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

Looks great! -- not sure we need the unstable feature here, but not opposed to being cautious either (: -- let's go ahead and merge!!

@yoshuawuyts yoshuawuyts merged commit 38a8676 into master Aug 30, 2019
@ghost ghost deleted the future-timeout branch August 31, 2019 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants