Skip to content

Commit 2557fc2

Browse files
authored
feat: support default options for tcp sockets/servers (#2241)
* feat: support default options for tcp sockets/servers Allow disabling Nagle's algorithm from the TCP factory function for lower latency at the slight expense of throughput when lots of small packets are sent. ```js tcp({ listenOpts: { // net.connect options here }, dialOpts: { // net.createServer options here } }) ```
1 parent 3dee5df commit 2557fc2

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

packages/transport-tcp/src/index.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,40 @@ export interface TCPOptions {
101101
* Open server (start listening for new connections) if connections fall below a limit.
102102
*/
103103
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
104+
105+
/**
106+
* Options passed to `net.connect` for every opened TCP socket
107+
*/
108+
dialOpts?: TCPSocketOptions
109+
110+
/**
111+
* Options passed to every `net.createServer` for every TCP server
112+
*/
113+
listenOpts?: TCPSocketOptions
104114
}
105115

106116
/**
107117
* Expose a subset of net.connect options
108118
*/
109119
export interface TCPSocketOptions extends AbortOptions {
120+
/**
121+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
122+
*/
110123
noDelay?: boolean
124+
125+
/**
126+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
127+
*/
111128
keepAlive?: boolean
129+
130+
/**
131+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
132+
*/
112133
keepAliveInitialDelay?: number
134+
135+
/**
136+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/net.html#new-netsocketoptions
137+
*/
113138
allowHalfOpen?: boolean
114139
}
115140

@@ -205,21 +230,24 @@ class TCP implements Transport {
205230

206231
return new Promise<Socket>((resolve, reject) => {
207232
const start = Date.now()
208-
const cOpts = multiaddrToNetConfig(ma) as (IpcSocketConnectOpts & TcpSocketConnectOpts)
209-
const cOptsStr = cOpts.path ?? `${cOpts.host ?? ''}:${cOpts.port}`
233+
const cOpts = multiaddrToNetConfig(ma, {
234+
...(this.opts.dialOpts ?? {}),
235+
...options
236+
}) as (IpcSocketConnectOpts & TcpSocketConnectOpts)
210237

211-
this.log('dialing %j', cOpts)
238+
this.log('dialing %a', ma)
212239
const rawSocket = net.connect(cOpts)
213240

214241
const onError = (err: Error): void => {
242+
const cOptsStr = cOpts.path ?? `${cOpts.host ?? ''}:${cOpts.port}`
215243
err.message = `connection error ${cOptsStr}: ${err.message}`
216244
this.metrics?.dialerEvents.increment({ error: true })
217245

218246
done(err)
219247
}
220248

221249
const onTimeout = (): void => {
222-
this.log('connection timeout %s', cOptsStr)
250+
this.log('connection timeout %a', ma)
223251
this.metrics?.dialerEvents.increment({ timeout: true })
224252

225253
const err = new CodeError(`connection timeout after ${Date.now() - start}ms`, 'ERR_CONNECT_TIMEOUT')
@@ -228,13 +256,13 @@ class TCP implements Transport {
228256
}
229257

230258
const onConnect = (): void => {
231-
this.log('connection opened %j', cOpts)
259+
this.log('connection opened %a', ma)
232260
this.metrics?.dialerEvents.increment({ connect: true })
233261
done()
234262
}
235263

236264
const onAbort = (): void => {
237-
this.log('connection aborted %j', cOpts)
265+
this.log('connection aborted %a', ma)
238266
this.metrics?.dialerEvents.increment({ abort: true })
239267
rawSocket.destroy()
240268
done(new AbortError())
@@ -273,6 +301,7 @@ class TCP implements Transport {
273301
*/
274302
createListener (options: TCPCreateListenerOptions): Listener {
275303
return new TCPListener({
304+
...(this.opts.listenOpts ?? {}),
276305
...options,
277306
maxConnections: this.opts.maxConnections,
278307
backlog: this.opts.backlog,

packages/transport-tcp/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function multiaddrToNetConfig (addr: Multiaddr, config: NetConfig = {}):
2222
}
2323

2424
// tcp listening
25-
return { ...addr.toOptions(), ...config }
25+
return { ...config, ...addr.toOptions() }
2626
}
2727

2828
export function getMultiaddrs (proto: 'ip4' | 'ip6', ip: string, port: number): Multiaddr[] {

0 commit comments

Comments
 (0)