Summary
Stabilize TcpStream.set_keepalive
, and add a method, is_closed
to check for connection status.
Motivation
Keep-alive is a common feature used in TCP, and should be exposed. Likewise, knowing when a TcpStream is dead is beneficial to know before reading so a new connection can be made. It’s currently possible to shutdown
a TcpStream, but a later function is not able to query if the stream has been shutdown other than to try to read
from it like business as usual.
Detailed design
Adjust and stabilize set_keepalive
to this, following the example from the socket timeouts RFC:
impl TcpStream {
fn set_keepalive(&mut self, duration: Duration) -> io::Result() { ... }
}
Separately, add a method to query the status of the connection. Being able to do this without reading out bytes would be useful, so as to know whether to continue to use this Stream, or to create a new one, before trying to read bytes out of it, or passing the stream to a more generic read method not designed to reconnect.
impl TcpStream {
fn is_closed(&self) -> io::Result<bool> { ... }
}
The other methods that close/shutdown a Stream could set state on the TcpStream itself, and is_closed
could short circuit based on that state, before trying a poll
on the socket.
Drawbacks
Alternatives
Unresolved questions
Should is_closed
return just a bool
instead of a Result
? If there is any error case, it can most likely be considered closed by the user. Any error they might care about should show it’s head when they try to TcpStream::connect
again.
Similar to the sockets timeout RFC, would it make more sense for set_keepalive
to receive a Option<Duration>
, so turning off keepalive can be done by passing None
, instead of Duration::zero()
?