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
Conversation
## 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
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
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... |
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.
Thanks! I have some comments regarding the documentation. The implementation looks good.
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Motivation
The
mpsc::Sender::reserve
method currently returns a permit that borrowsfrom the
Sender
. It would be nice to have a version of it that returnsan owned permit.
Solution
This branch adds an
OwnedPermit
type andSender::{reserve_owned, try_reserve_owned}
methods. Unlike the comparable methods onSemaphore
, these methods do not require anArc<Sender>
as thereceiver; this is because the sender internally reference counts the
channel and is already cheap to clone. Requiring an
Arc
would simplyadd 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 abilityto return the
Sender
from a successfulOwnedPermit::send
. Thisallows them to be used without additional clones. Essentially, this is a
very simple type-level encoding of the sender's state, with the
transitions
Additionally, I added an
OwnedPermit::release
, which returns theSender
and releases the permit without sending a message.Closes #3688