tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Add method `mpsc::Receiver::is_closed`

Open Nugine opened this issue 3 years ago • 7 comments

Is your feature request related to a problem? Please describe.

Describe the solution you'd like Add a method to check whether the mpsc channel is closed from rx side. mpsc::Receiver::is_closed https://docs.rs/tokio/1.17.0/tokio/sync/mpsc/struct.Sender.html#method.is_closed

Describe alternatives you've considered

Additional context

Nugine avatar Apr 25 '22 13:04 Nugine

Hi Nugine,

Please can you expand on your use case/scenario where having this method would provide a better solution than the existing mechanism of calling recv() and checking for None ?

Ian

IanMichaelAsh avatar Apr 26 '22 07:04 IanMichaelAsh

In general, I am ok with adding this assuming that it is easy to add.

Darksonn avatar Apr 26 '22 08:04 Darksonn

I need to check whether a command channel is closed before trying to reconnect to a remote server. I think it is easy to add because mpsc::Sender already has the method is_closed.

Nugine avatar Apr 26 '22 08:04 Nugine

@Darksonn Obviously from your comment you can't be certain, but would your assumption lean toward most likely it is easy to add? I'd love to contribute to Tokio, so maybe this a good starting point, or would you point me elsewhere?

IanMichaelAsh avatar Apr 26 '22 09:04 IanMichaelAsh

My guess is that it is easy, yes. You're welcome to submit a PR for this. You might also want to look in the help wanted tag for easy ones, though note that the older ones might be outdated. If you have questions on how to approach writing the PR, you can ask in our discord server (or here).

Darksonn avatar Apr 26 '22 16:04 Darksonn

As a use case, I have a trait that implements reader and writer functionality similar to TcpStream, UnixStream, and the Windows client/server pipes. The trait provides try_write, try_read, and most importantly an equivalent to ready.

For one implementation, it uses mpsc Sender and Receiver.

It's easy to check if Interest::WRITEABLE will return Ready::WRITE_CLOSED or Ready::WRITEABLE by using tx.is_closed(). It's more difficult to check if Interest::READABLE will return Ready::READ_CLOSED or Ready::READABLE since there is no rx.is_closed(). Instead, I have to do rx.try_recv to check if the receiver is disconnected. If not and I receive data, I have to store this somewhere for future try_read calls.

chipsenkbeil avatar Sep 10 '22 23:09 chipsenkbeil

I would be interested in picking this up -- this change seems more than just adding a single method so will be reaching out to you folks on Discord.

sanjayts avatar Sep 14 '22 18:09 sanjayts