tokio
tokio copied to clipboard
Add method `mpsc::Receiver::is_closed`
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
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
In general, I am ok with adding this assuming that it is easy to add.
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.
@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?
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).
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.
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.