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

6566 - Add 'run_until_cancelled' to 'tokio_util::sync::CancellationToken' #6618

Merged

Conversation

tglane
Copy link
Contributor

@tglane tglane commented Jun 6, 2024

Motivation

Citing from #6566:

Give how (seemingly) often a tokio_util::sync::CancellationToken gets used as follows:
tokio::select! {
_ = token.cancelled() => { /* something / }
other => other_future => { /
something else */ }
}
I propose adding a method to CancellationToken that takes a Future, runs it to completion, and returns its output — unless the token is cancelled before the future completes, in which case the future is dropped. I feel the best type for the result of this composed future would be an OptionFut::Output, with None indicating cancellation.

Solution

I added the described function to tokio_util::sync::CancellationToken and added a test case to the loom tests.
I also had to add the macros feature of Tokio to get access to the select! macro. I thought about using select! provided by futures_util but that would require WaitForCancellationFuture to implement FusedFuture so I had to stick with the tokio one. But maybe this is a not worthy trade off just for a convenience function?

This would close #6566

@tglane tglane changed the title 6566 cancellation token run until cancelled 6566 - Add 'run_until_cancelled' to 'tokio_util::sync::CancellationToken' Jun 6, 2024
@tglane tglane force-pushed the 6566-cancellation-token-run-until-cancelled branch from f1763ca to 58406d8 Compare June 6, 2024 17:07
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync labels Jun 6, 2024
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Looks pretty good overall!

tokio-util/src/sync/cancellation_token.rs Outdated Show resolved Hide resolved
tokio-util/src/sync/cancellation_token.rs Outdated Show resolved Hide resolved
…where it is used and use 'WaitForCancellationFuture'
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Looks good to me. I only have two remaining things, and otherwise this is ready to be merged.

tokio-util/src/sync/cancellation_token.rs Outdated Show resolved Hide resolved
tokio-util/src/sync/cancellation_token.rs Outdated Show resolved Hide resolved
tglane and others added 2 commits June 10, 2024 19:42
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thank you.

@Darksonn Darksonn merged commit 53ea44b into tokio-rs:master Jun 13, 2024
77 checks passed
@tglane tglane deleted the 6566-cancellation-token-run-until-cancelled branch June 13, 2024 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a CancellationToken method for running a future until completion or cancellation
2 participants