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

sync: add mpsc::Sender::{reserve_owned, try_reserve_owned} #3704

Merged
merged 4 commits into from Apr 15, 2021

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Apr 14, 2021

Motivation

The mpsc::Sender::reserve method currently returns a permit that borrows
from the Sender. It would be nice to have a version of it that returns
an owned permit.

Solution

This branch adds an OwnedPermit type and Sender::{reserve_owned, try_reserve_owned} methods. Unlike the comparable methods on
Semaphore, these methods do not require an Arc<Sender> as the
receiver; this is because the sender internally reference counts the
channel and is already cheap to clone. Requiring an Arc would simply
add an unnecessary second layer of reference counting, which is not
ideal; instead, the documentation encourages the user to clone the
sender prior to calling reserve_owned when necessary.

Since these methods take the Sender by value, they also have the ability
to return the Sender from a successful OwnedPermit::send. This
allows them to be used without additional clones. Essentially, this is a
very simple type-level encoding of the sender's state, with the
transitions

      ┌──────┐
 ┌───►│Sender├───┐
 │    └──────┘   │
send          reserve
 │ ┌───────────┐ │
 └─┤OwnedPermit│◄┘
   └───────────┘

Additionally, I added an OwnedPermit::release, which returns the
Sender and releases the permit without sending a message.

Closes #3688

## Motivation

The `mpsc::Sender::reserve` method currently returns a permit that borrows
from the `Sender`. It would be nice to have a version of it that returns
an owned permit.

## Solution

This branch adds an `OwnedPermit` type and `Sender::{reserve_owned,
try_reserve_owned}` methods. Unlike the comparable methods on
`Semaphore`, these methods do *not* require an `Arc<Sender>` as the
receiver; this is because the sender internally reference counts the
channel and is already cheap to clone. Requiring an `Arc` would simply
add an unnecessary second layer of reference counting, which is not
ideal; instead, the documentation encourages the user to clone the
sender prior to calling `reserve_owned` when necessary.

Since these methods take the `Sender` by value, they also have the ability
to _return_ the `Sender` from a successful `OwnedPermit::send`. This
allows them to be used without additional clones. Essentially, this is a
very simple type-level encoding of the sender's state, with the
transitions
```
      ┌──────┐
 ┌───►│Sender├───┐
 │    └──────┘   │
send          reserve
 │ ┌───────────┐ │
 └─┤OwnedPermit│◄┘
   └───────────┘
```

Additionally, I added an `OwnedPermit::release`, which returns the
`Sender` and releases the permit *without* sending a message.

Closes #3688
@hawkw hawkw requested a review from Darksonn April 14, 2021 22:41
@hawkw hawkw self-assigned this Apr 14, 2021
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw
Copy link
Member Author

hawkw commented Apr 14, 2021

Honestly, most of the time I spent on this was trying to write docs that actually express how this should be used --- the implementation was pretty easy. Hopefully the docs are helpful for new users...

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Apr 15, 2021
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.

Thanks! I have some comments regarding the documentation. The implementation looks good.

tokio/src/sync/mpsc/bounded.rs Outdated Show resolved Hide resolved
tokio/src/sync/mpsc/bounded.rs Outdated Show resolved Hide resolved
tokio/src/sync/mpsc/bounded.rs Show resolved Hide resolved
tokio/src/sync/mpsc/bounded.rs Show resolved Hide resolved
tokio/src/sync/mpsc/bounded.rs Show resolved Hide resolved
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw requested a review from Darksonn April 15, 2021 18:04
@hawkw hawkw merged commit d6da67b into master Apr 15, 2021
@hawkw hawkw deleted the eliza/permit-owned branch April 15, 2021 19:55
@Darksonn Darksonn mentioned this pull request May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add owned permits to mpsc channel
2 participants