Skip to content

Commit

Permalink
fix: don't set TCP keepalive on listening sockets
Browse files Browse the repository at this point in the history
On Mac OS X, it causes the listening socket to be “closed” after the
keepalive timeout and future connections to fail with “connection
refused” errors. We suspect the same may happen on linux in some
circumstances because we see similar errors in CI.

For hyper, we switch to use `AddrIncoming::set_keepalive`. It sets TCP
keepalive options on sockets returned by `accept` instead of on the
listening socket.

Tentacle unfortunately does not seem to allow setting options on
accepted sockets, so TCP keepalive setting is removed for now.
  • Loading branch information
blckngm authored and jjyr committed May 30, 2022
1 parent ccce5bf commit 70c341f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
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)?)
let server = Server::builder(incoming)
.tcp_nodelay(true)
.serve(make_service_fn(move |_| {
let rpc_server = Arc::clone(&rpc_server);
Expand Down

0 comments on commit 70c341f

Please sign in to comment.