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

fix: don't set TCP keepalive on listening sockets #709

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions crates/p2p-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashSet, sync::Arc, time::Duration};

use anyhow::{Context, Result};
use gw_config::P2PNetworkConfig;
use socket2::{SockRef, TcpKeepalive};
use socket2::SockRef;
use tentacle::{
async_trait,
builder::ServiceBuilder,
Expand Down Expand Up @@ -42,12 +42,6 @@ impl P2PNetwork {
let sock_ref = SockRef::from(&socket);
sock_ref.set_reuse_address(true)?;
sock_ref.set_nodelay(true)?;
sock_ref.set_tcp_keepalive(
&TcpKeepalive::new()
.with_interval(Duration::from_secs(15))
.with_time(Duration::from_secs(5))
.with_retries(3),
)?;
Ok(socket)
})
// TODO: allow config keypair.
Expand Down
13 changes: 4 additions & 9 deletions crates/rpc-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@ pub async fn start_jsonrpc_server(
let rpc_server = registry.build_rpc_server()?;

let listener = TcpListener::bind(listen_addr).await?;
let listener_ref = socket2::SockRef::from(&listener);
// Set TCP keepalive options with socket2 because tokio/hyper does not support setting interval/retries (yet).
let keepalive = socket2::TcpKeepalive::new()
.with_time(Duration::from_secs(10))
.with_interval(Duration::from_secs(5))
.with_retries(3);
// TCP keepalive options set on listening sockets are inhereted by accepted sockets (at least on linux and FreeBSD).
listener_ref.set_tcp_keepalive(&keepalive)?;

// Format the full address.
let url = format!("http://{}", listener.local_addr()?);
log::info!("JSONRPC server listening on {}", url);

let mut incoming = AddrIncoming::from_listener(listener)?;
incoming.set_keepalive(Some(Duration::from_secs(10)));

// Start a hyper server.
let server = Server::builder(AddrIncoming::from_listener(listener)?)
Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/hyperium/hyper/blob/f8e2a831943e9a687a1b3277c806caca6e0ac8a4/src/server/tcp.rs#L48-L58

    /// Creates a new `AddrIncoming` from an existing `tokio::net::TcpListener`.
    pub fn from_listener(listener: TcpListener) -> crate::Result<Self> {
        let addr = listener.local_addr().map_err(crate::Error::new_listen)?;
        Ok(AddrIncoming {
            listener,
            addr,
            sleep_on_errors: true,
            tcp_keepalive_timeout: None,
            tcp_nodelay: false,
            timeout: None,
        })
    }

curious why hyper doesn't inherit the keepalive options from socket

let server = Server::builder(incoming)
.tcp_nodelay(true)
.serve(make_service_fn(move |_| {
let rpc_server = Arc::clone(&rpc_server);
Expand Down