Skip to content

Commit 13a870c

Browse files
authored
fix: disable Nagle's algorithm by default (#2242)
By default Node.js enables Nagle's algorithm. This adds a small delay when sending tiny buffers to batch-send them later. Unfortunately this makes protocol negotiation slower as the protocol strings are small enough to be batch sent. The change here is to turn it off by default, this reduces the perf test connection establisment from 1.2s to 0.8s. See the latency measurements in the "Connection Establishment" test [on this run](https://observablehq.com/@libp2p-workspace/performance-dashboard?branch=cac8364ac83a4d8856851687a605e50b1e3855fb) for an example.
1 parent 2557fc2 commit 13a870c

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

packages/transport-tcp/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class TCP implements Transport {
182182

183183
async dial (ma: Multiaddr, options: TCPDialOptions): Promise<Connection> {
184184
options.keepAlive = options.keepAlive ?? true
185+
options.noDelay = options.noDelay ?? true
185186

186187
// options.signal destroys the socket before 'connect' event
187188
const socket = await this._connect(ma, options)

packages/transport-tcp/src/listener.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
8383
super()
8484

8585
context.keepAlive = context.keepAlive ?? true
86+
context.noDelay = context.noDelay ?? true
8687

8788
this.log = context.logger.forComponent('libp2p:tcp:listener')
8889
this.addr = 'unknown'

0 commit comments

Comments
 (0)