Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 8dba9c7

Browse files
authored
feat: add socket backlog option (#263)
1 parent c72bad3 commit 8dba9c7

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export interface TCPOptions {
3636
*/
3737
maxConnections?: number
3838

39+
/**
40+
* Parameter to specify the maximum length of the queue of pending connections
41+
* https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
42+
*/
43+
backlog?: number
44+
3945
/**
4046
* Close server (stop listening for new connections) if connections exceed a limit.
4147
* Open server (start listening for new connections) if connections fall below a limit.
@@ -215,6 +221,7 @@ class TCP implements Transport {
215221
return new TCPListener({
216222
...options,
217223
maxConnections: this.opts.maxConnections,
224+
backlog: this.opts.backlog,
218225
closeServerOnMaxConnections: this.opts.closeServerOnMaxConnections,
219226
socketInactivityTimeout: this.opts.inboundSocketInactivityTimeout,
220227
socketCloseTimeout: this.opts.socketCloseTimeout,

src/listener.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface Context extends TCPCreateListenerOptions {
4141
socketInactivityTimeout?: number
4242
socketCloseTimeout?: number
4343
maxConnections?: number
44+
backlog?: number
4445
metrics?: Metrics
4546
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
4647
}
@@ -269,12 +270,13 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
269270

270271
const peerId = ma.getPeerId()
271272
const listeningAddr = peerId == null ? ma.decapsulateCode(CODE_P2P) : ma
273+
const { backlog } = this.context
272274

273275
this.status = {
274276
started: true,
275277
listeningAddr,
276278
peerId,
277-
netConfig: multiaddrToNetConfig(listeningAddr)
279+
netConfig: multiaddrToNetConfig(listeningAddr, { backlog })
278280
}
279281

280282
await this.netListen()

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const ProtoFamily = { ip4: 'IPv4', ip6: 'IPv6' }
88

99
export type NetConfig = ListenOptions | (IpcSocketConnectOpts & TcpSocketConnectOpts)
1010

11-
export function multiaddrToNetConfig (addr: Multiaddr): NetConfig {
11+
export function multiaddrToNetConfig (addr: Multiaddr, config: NetConfig = {}): NetConfig {
1212
const listenPath = addr.getPath()
1313

1414
// unix socket listening
@@ -22,7 +22,7 @@ export function multiaddrToNetConfig (addr: Multiaddr): NetConfig {
2222
}
2323

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

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

0 commit comments

Comments
 (0)