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

Add TCP keepalive option #143

Merged
merged 2 commits into from
Mar 22, 2023
Merged

Add TCP keepalive option #143

merged 2 commits into from
Mar 22, 2023

Conversation

kgraefe
Copy link
Contributor

@kgraefe kgraefe commented Mar 17, 2023

No description provided.

Comment on lines +128 to +144
if status == PendingStatus::Ready {
if let Some(keepalive) = &self.keepalive {
#[cfg(target_os = "windows")]
let socket = unsafe { Socket::from_raw_socket(self.stream.as_raw_socket()) };
#[cfg(not(target_os = "windows"))]
let socket = unsafe { Socket::from_raw_fd(self.stream.as_raw_fd()) };

if let Err(e) = socket.set_tcp_keepalive(keepalive) {
log::warn!("TCP set keepalive error: {}", e);
}

// Don't drop so the underlying socket is not closed.
forget(socket);
}
}

status
Copy link
Owner

Choose a reason for hiding this comment

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

I'm wondering why do this here instead of in connect()/listen() methods.

Could we get rid of the unsafe code building the socket with socket2, and once it's built, get the stream? instead of building the stream and getting the socket from it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot do that in listen() because it concerns the accepted socket, not the listener socket. We could do it in accept() but mio does quite a bit of platform dependent stuff in the UNIX path which contains unsafe as well. We would loose that or have to copy and maintain it ourselves. I'd rather avoid that and live with the unsafe code.

(The Windows path just calls std::net::TcpListener::accept(), though.)

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, thanks for your thoughts!

The reason the actual work is done when pending() returns
PendingStatus::Ready is that on Windows the option cannot be set during
connect (when connect() was called but the socket is not yet connected).
Also this way it applies to both outgoing and incoming connections.

Signed-off-by: Konrad Gräfe <kgraefe@paktolos.net>
Signed-off-by: Konrad Gräfe <kgraefe@paktolos.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants