Skip to content
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

net: expose keepalive configuration on TcpSocket #3146

Closed
wants to merge 4 commits into from

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Nov 16, 2020

Depends on #3145

Motivation

Tokio v0.3 no longer exposes a way to configure TCP keepalive probes.
Many users need this functionality, so we want to re-expose keepalive
configuration via TcpSocket.

Solution

This commit exposes the TCP keepalive configuration added to mio's
TcpSocket in tokio-rs/mio#1385. This includes a method for enabling
keepalvie and checking if keepalive is enabled, a builder interface for
configuring keepalive parameters (keepalive time, the amount of time
before keepalive probes are sent, keepalive interval, the time between
each probe, and keepalive retries, the number of failed probes before a
connection is closed), and accessors for the values of keepalive
parameters. Due to Windows API limitations, mio has to provide a
builder for configuring keepalive parameters, since they must all be set
in one API call. Therefore, we re-export the keepalive builder struct.
If the mio API changes, we should be able to replace this with our own
type transparently, so it didn't seem necessary to wrap this --- if I'm
wrong, please let me know!

Fixes: #3082

This commit adds `set_{send, recv}_buffer_size` methods to `TcpSocket`
for setting the size of the TCP send and receive buffers, and `{send,
recv}_buffer_size` methods for returning the current value. These just
call into similar methods on `mio`'s `TcpSocket` type, which were added
in tokio-rs/mio#1384.

Refs: #3082

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw requested review from carllerche and Darksonn November 16, 2020 20:32
@hawkw hawkw changed the title Eliza/tcp keepalive net: expose keepalive configuration on TcpSocket Nov 16, 2020
/// keepalive parameters after they have been set..
///
/// Some platforms specify this value in seconds, so sub-second
/// specifications may be omitted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rounded down or up? What happens if this rounds to zero?

@@ -86,6 +87,11 @@ cfg_net! {
}
}

cfg_net! {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use mio::net::TcpKeepalive;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a mio type part of the public API, which is probably not ideal.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to add this API at this time. This goes beyond what Tokio has done to date with regard to socket options. Until now, we have added socket option APIs that are essentially pass-through to the OS call. This attempts to provide a portable API that abstracts differences.

I also see a potential issue with the implementation in Mio where a single call to set_keepalive_params may result in multiple sub calls. If the first sub call succeeds, but the second fails, the socket state is inconsistent. All other socket option setters are atomic.

Perhaps it is best to leave such configuration to other crates or the user to call themselves?

@@ -86,6 +87,11 @@ cfg_net! {
}
}

cfg_net! {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use mio::net::TcpKeepalive;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not exposing mio as part of Tokio's public API. If we do this, we will need to re-define the type.

@@ -33,7 +33,7 @@ cfg_net! {

pub mod tcp;
pub use tcp::listener::TcpListener;
pub use tcp::socket::TcpSocket;
pub use tcp::socket::{TcpSocket, TcpKeepalive};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably can keep this type in tokio::net::tcp and not re-export it.

@carllerche
Copy link
Member

Closing as we decided in Discord to punt this.

@carllerche carllerche closed this Nov 17, 2020
@hawkw
Copy link
Member Author

hawkw commented Nov 17, 2020

@carllerche wdyt about exposing just keepalive time (time to the initial probe)? That, at least, can be configured consistently across all platforms.

@carllerche
Copy link
Member

If it is consistent across platforms then it seems fine to me.

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-net Module: tokio/net labels Nov 22, 2020
@Darksonn Darksonn deleted the eliza/tcp-keepalive branch November 22, 2020 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-net Module: tokio/net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

net: expose more socket options on TcpSocket
4 participants