Skip to content

Commit 1c5037a

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 ccdb399 commit 1c5037a

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
@@ -793,6 +793,7 @@ impl RPC {
793793
stream
794794
.set_nonblocking(false)
795795
.expect("failed to set connection as blocking");
796+
stream.set_nodelay(true).expect("failed to set TCP_NODELAY");
796797
if acceptor.send(Some((stream, addr))).is_err() {
797798
break; // receiver dropped, server is shutting down
798799
}

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)