Skip to content

Add Receiver::new_sender to channels#750

Open
oowekyala wants to merge 4 commits intocrossbeam-rs:masterfrom
oowekyala:reconnectable-channel
Open

Add Receiver::new_sender to channels#750
oowekyala wants to merge 4 commits intocrossbeam-rs:masterfrom
oowekyala:reconnectable-channel

Conversation

@oowekyala
Copy link
Copy Markdown

@oowekyala oowekyala commented Oct 29, 2021

This change gives 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 adds support for creating new Sender instances from the Receiver directly. 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. The new method Receiver::new_sender is usable on bounded and unbounded channels; it panics on tick, at and never channels.

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
@ryoqun
Copy link
Copy Markdown
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
Copy Markdown
Contributor

ryoqun commented Jan 9, 2024

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

@taiki-e taiki-e added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author label Feb 21, 2026
oowekyala and others added 2 commits February 25, 2026 13:08
Just put the new_sender function on regular Receiver.
@oowekyala oowekyala force-pushed the reconnectable-channel branch from 1f73775 to 1a5820e Compare February 25, 2026 13:26
@oowekyala oowekyala force-pushed the reconnectable-channel branch from 1a5820e to 04cc29d Compare February 25, 2026 13:28
@taiki-e taiki-e removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author label Feb 27, 2026
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