tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Support ready() for mpsc::Receiver

Open shade opened this issue 11 months ago • 4 comments

Is your feature request related to a problem? Please describe. I have an application that needs to wait for a value to show up in a channel within a certain time-frame before running some business logic.

Describe the solution you'd like The ideal solution would be to have a async peek() functionality which is ready when a value has been sent but does not consume the value.

let (sender, receiver) = mpsc::channel(10);

timeout(receiver.peek(), TIMEOUT).await?;

// Hand off the receiver to some task to process all the value.
business_logic_task(receiver);

Describe alternatives you've considered Alternative 1. Is to await on recv() and write our business logic to work with the first channel value as a special case.

Alternative 2. Is to wrap the Receiver in a ReceiverStream, convert it into a Peekable stream and call peek on that.

My main gripe is that after the conversion to Peekable, we cannot get the original receiver back as we have a lot of logic that depends on Receiver type and we will have to change this to be Peekable or worse, a generic Stream.

shade avatar Nov 09 '24 23:11 shade