-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libs] Add TcpStream::keepalive
and TcpStream::set_keepalive
#69774
Comments
To recap: the current limitation here is that Windows doesn't seem to provide a method for retrieving the socket timeout. For example this code: let stream = TcpStream::connect("104.198.14.52:80")?;
stream.set_keepalive(Some(Duration::from_secs(1)))?;
println!("{:?}", stream.keepalive()?); Will print |
Note that |
|
TcpStream::keepalive
and TcpStream::set_keepalive
Did Tokio get rid of set_keepalive for TcpStream on v1.0 on purpose? |
Hi, set_keepalive is an important feature. It lets a TcpStream detect a disconnection due to a network problem occurring before the TcpStream being correctly closed from any side. let stream : tokio::net::TcpStream = listener.accpt().await?;
let stream: std::net::TcpStream = stream.into_std().unwrap();
let socket: socket2::Socket = socket2::Socket::from(stream);
let keepalive = TcpKeepalive::new()
.with_time(Duration::from_secs(4))
.with_interval(Duration::from_secs(1))
.with_retries(4);
socket.set_tcp_keepalive(&keepalive)?;
let stream: std::net::TcpStream = socket.into();
let stream: tokio::net::TcpStream = tokio::net::TcpStream::from_std(stream).unwrap(); instead of let stream : tokio::net::TcpStream = listener.accpt().await?;
let keepalive = TcpKeepalive::new()
.with_time(Duration::from_secs(4))
.with_interval(Duration::from_secs(1))
.with_retries(4);
stream.set_tcp_keepalive(&keepalive)?; By the way TcpStream has a lot of functions which are marked as "Only for OS..." |
I really want to see But you should also take a look at |
I found the PR that removed
But why were they removed from Mio ? The answer can be found in this commit :
So I think these methods have just been forgotten since then. The good point is that TCP keepalive support wasn't removed because it would be a bad idea, it was removed because socket2 already support them better |
Really interesting, thanks |
Hello. I would like to add my two bits into the add keepalive debate camp. I've been working in a number of languages recently. C/C++ and Flutter both have keepalive functionality. Heck, Websockets.io for the web and Node have keepalive functionality. Not many network paradigms nowadays call for continuous connections, but in cases where they are desired, keepalive is a vital element. |
Kind of surprised this is still pending years later, is a PR all that's missing to make this happen? |
I think the question is how to handle the case of Windows. But if there was a way to perhaps import a trait from one of the Does that make sense to do? If so, I'd also be interested in a PR. |
Today an issue was filed in
async-std
asking how to set theSO_KEEPALIVE
option on ourTcpStream
. Because we've only implemented methods that are available instd
as well, we never added these methods. But I think there's a strong case to add these methods tostd::net::TcpStream
.TcpStream
already supports lots of control methods natively including:set_read_timeout
,set_write_timeout
,set_ttl
, andset_nodelay
.set_keepalive
would be a method along the same lines providing a convenient way to set yet another low-level option. And there's a clear need for it: at least 3 other low-level TCP related libraries exposekeepalive
andset_keepalive
(see the "Prior Art" section for more on this).Implementation
The implementation can probably entirely copied from
net2::TcpStreamExt
. Unlike some of the other methods in that genre, these methods can operate entirely on an already instantiated socket.Also between all existing implementations that I've found, there's consensus that the shape of the methods should be as follows:
Prior Art
net2::TcpStreamExt
exposes bothkeepalive
andset_keepalive
.tokio::net::TcpStream
exposes bothkeepalive
andset_keepalive
.socket2::Socket
exposes bothkeepalive
andset_keepalive
.Prior Discussion
I couldn't find any references to
TcpStream::{keepalive,set_keepalive}
directly but #27773 seems to touch on the general topic. Also #28339 introduced many of the socket controls mentioned earlier, but doesn't seem to mentionkeepalive
either.edit: I've since found #31945 (comment) and #24594 which mention
TcpStream::keepalive
. In particular #31945 (comment) mentions:Discussion seems to have been started in deprecrated/net2-rs#29, but hasn't progressed since 2016.
Drawbacks
I don't think there are any. Back in 2015 it seems these methods weren't added as part of Rust 1.0 because the libs team wanted to be cautious of what to include. But I think in the years since it's proven to be a useful addition that's seen a lot of use throughout the ecosystem, and now might be a good time to consider adding these to
std
.The text was updated successfully, but these errors were encountered: