Skip to content

Commit ecc1709

Browse files
committed
perf(tcp): disable Nagle's algorithm on accepted connections
Set TCP_NODELAY on all accepted TCP connections for both the Electrum RPC server and the REST HTTP server. Nagle's algorithm interacts poorly with TCP delayed ACKs on request/response protocols, adding up to 200ms of latency per round trip on otherwise idle connections. Unix socket path in rest.rs is correctly left unchanged as TCP_NODELAY does not apply to Unix domain sockets.
1 parent 69fcfc5 commit ecc1709

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/electrum/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ impl RPC {
789789
stream
790790
.set_nonblocking(false)
791791
.expect("failed to set connection as blocking");
792+
stream.set_nodelay(true).expect("failed to set TCP_NODELAY");
792793
acceptor.send(Some((stream, addr))).expect("send failed");
793794
}
794795
});

src/rest.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,10 @@ async fn run_server(config: Arc<Config>, query: Arc<Query>, rx: oneshot::Receive
601601
tokio::select! {
602602
result = listener.accept() => {
603603
match result {
604-
Ok((stream, _)) => spawn_conn(stream, Arc::clone(&query), Arc::clone(&config), &graceful),
604+
Ok((stream, _)) => {
605+
stream.set_nodelay(true).expect("failed to set TCP_NODELAY");
606+
spawn_conn(stream, Arc::clone(&query), Arc::clone(&config), &graceful);
607+
}
605608
Err(e) => warn!("accept error: {}", e),
606609
}
607610
}

0 commit comments

Comments
 (0)