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

Commit

Permalink
feat: add socket backlog option (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Apr 12, 2023
1 parent c72bad3 commit 8dba9c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export interface TCPOptions {
*/
maxConnections?: number

/**
* Parameter to specify the maximum length of the queue of pending connections
* https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
*/
backlog?: number

/**
* Close server (stop listening for new connections) if connections exceed a limit.
* Open server (start listening for new connections) if connections fall below a limit.
Expand Down Expand Up @@ -215,6 +221,7 @@ class TCP implements Transport {
return new TCPListener({
...options,
maxConnections: this.opts.maxConnections,
backlog: this.opts.backlog,
closeServerOnMaxConnections: this.opts.closeServerOnMaxConnections,
socketInactivityTimeout: this.opts.inboundSocketInactivityTimeout,
socketCloseTimeout: this.opts.socketCloseTimeout,
Expand Down
4 changes: 3 additions & 1 deletion src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface Context extends TCPCreateListenerOptions {
socketInactivityTimeout?: number
socketCloseTimeout?: number
maxConnections?: number
backlog?: number
metrics?: Metrics
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
}
Expand Down Expand Up @@ -269,12 +270,13 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene

const peerId = ma.getPeerId()
const listeningAddr = peerId == null ? ma.decapsulateCode(CODE_P2P) : ma
const { backlog } = this.context

this.status = {
started: true,
listeningAddr,
peerId,
netConfig: multiaddrToNetConfig(listeningAddr)
netConfig: multiaddrToNetConfig(listeningAddr, { backlog })
}

await this.netListen()
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ProtoFamily = { ip4: 'IPv4', ip6: 'IPv6' }

export type NetConfig = ListenOptions | (IpcSocketConnectOpts & TcpSocketConnectOpts)

export function multiaddrToNetConfig (addr: Multiaddr): NetConfig {
export function multiaddrToNetConfig (addr: Multiaddr, config: NetConfig = {}): NetConfig {
const listenPath = addr.getPath()

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

// tcp listening
return addr.toOptions()
return { ...addr.toOptions(), ...config }
}

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

0 comments on commit 8dba9c7

Please sign in to comment.