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

Add Receiver::new_sender to channels #750

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

oowekyala
Copy link

This change gives some channel::Receivers the ability to spawn new senders at will.

Currently, Receivers are disconnected as soon as the Sender count reaches zero, as new Senders can only be created by cloning an existing one. Disconnected receivers do not block on recv and such, instead they just fail immediately.

This didn't quite fit my use case, where I need to have the ability to create new Senders when I want to, and also get the non-blocking behavior if there is no Sender alive. With the current API, I have to keep a prototype Sender which is always alive and which I can clone whenever I want. The problem is that since the prototype is always alive, the channel never gets disconnected. That means calls to recv stay blocking, even when we know that no message will ever be sent through the Sender prototype, because its only purpose is to produce new Senders.

This change creates a new Receiver type that supports creating new Sender instances. That means, I don't need to keep a Sender prototype anymore, so my Receiver is non-blocking when I know there is no real Sender alive. But even after the channel was disconnected, you can "reconnect" it by spawning new senders.

Obviously this method cannot appear in the API of channel flavours like tick or never, since the point of those is to expose only a receiver. This means, we need two different types for Receiver. I put that new API in a separate submodule so that it's easy to import and document. That submodule only supports creating unbounded channels for now, as that was my use case. If you're interested we may be able to add support for bounded and zero channels, but there is AFAIK no risk in keeping an incomplete API here.

Please let me know if there is anything you need from me to get this merged faster, be it documentation, tests, etc.
Cheers :)

@oowekyala oowekyala changed the title Make channels reconnectable Add Receiver::new_sender to channels Oct 29, 2021
todo!() wasn't stable back in 1.36.0.
I feel like unreachable!() is more appropriate
anyway, as these code branches are unreachable
until we implement the corresponding public
function `bounded`.
crossbeam-channel/src/channel.rs Outdated Show resolved Hide resolved
crossbeam-channel/src/channel.rs Outdated Show resolved Hide resolved
@ryoqun
Copy link
Contributor

ryoqun commented Jan 4, 2024

hey, this api is interesting to me as well.

Obviously this method cannot appear in the API of channel flavours like tick or never, since the point of those is to expose only a receiver. This means, we need two different types for Receiver. I put that new API in a separate submodule so that it's easy to import and document. That submodule only supports creating unbounded channels for now, as that was my use case. If you're interested we may be able to add support for bounded and zero channels, but there is AFAIK no risk in keeping an incomplete API here.

However, I didn't particularly like this submodule arrangement... How about making new_sender returning some dummy sender for tick and never? maybe this will be called full() or just disconnected() #1047 (comment)?

@ryoqun
Copy link
Contributor

ryoqun commented Jan 9, 2024

ping @oowekyala @taiki-e regarding my api idea: #750 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants