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

Commit b155bf6

Browse files
committed
Apply PR feedback
1 parent 83367e2 commit b155bf6

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as mafmt from '@multiformats/mafmt'
33
import errCode from 'err-code'
44
import { logger } from '@libp2p/logger'
55
import { toMultiaddrConnection } from './socket-to-conn.js'
6-
import { LimitServerConnectionsOpts, TCPListener } from './listener.js'
6+
import { CloseServerOnMaxConnectionsOpts, TCPListener } from './listener.js'
77
import { multiaddrToNetConfig } from './utils.js'
88
import { AbortError } from '@libp2p/interfaces/errors'
99
import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
@@ -41,7 +41,7 @@ export interface TCPOptions {
4141
* Close server (stop listening for new connections) if connections exceed a limit.
4242
* Open server (start listening for new connections) if connections fall below a limit.
4343
*/
44-
limitServerConnections?: LimitServerConnectionsOpts
44+
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
4545
}
4646

4747
/**
@@ -197,7 +197,7 @@ class TCP implements Transport {
197197
return new TCPListener({
198198
...options,
199199
maxConnections: this.opts.maxConnections,
200-
limitServerConnections: this.opts.limitServerConnections,
200+
closeServerOnMaxConnections: this.opts.closeServerOnMaxConnections,
201201
socketInactivityTimeout: this.opts.inboundSocketInactivityTimeout,
202202
socketCloseTimeout: this.opts.socketCloseTimeout,
203203
metrics: this.components.metrics

src/listener.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ async function attemptClose (maConn: MultiaddrConnection) {
2727
}
2828
}
2929

30-
export interface LimitServerConnectionsOpts {
31-
acceptBelow: number
32-
rejectAbove: number
30+
export interface CloseServerOnMaxConnectionsOpts {
31+
/** Server listens once connection count is less than `listenBelow` */
32+
listenBelow: number
33+
/** Close server once connection count is greater than or equal to `closeAbove` */
34+
closeAbove: number
3335
onListenError?: (err: Error) => void
3436
}
3537

@@ -40,7 +42,7 @@ interface Context extends TCPCreateListenerOptions {
4042
socketCloseTimeout?: number
4143
maxConnections?: number
4244
metrics?: Metrics
43-
limitServerConnections?: LimitServerConnectionsOpts
45+
closeServerOnMaxConnections?: CloseServerOnMaxConnectionsOpts
4446
}
4547

4648
const SERVER_STATUS_UP = 1
@@ -82,6 +84,13 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
8284
this.server.maxConnections = context.maxConnections
8385
}
8486

87+
if (context.closeServerOnMaxConnections != null) {
88+
// Sanity check options
89+
if (context.closeServerOnMaxConnections.closeAbove < context.closeServerOnMaxConnections.listenBelow) {
90+
throw Error('closeAbove must be >= listenBelow')
91+
}
92+
}
93+
8594
this.server
8695
.on('listening', () => {
8796
if (context.metrics != null) {
@@ -174,16 +183,16 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
174183
this.connections.delete(maConn)
175184

176185
if (
177-
this.context.limitServerConnections != null &&
178-
this.connections.size < this.context.limitServerConnections.acceptBelow
186+
this.context.closeServerOnMaxConnections != null &&
187+
this.connections.size < this.context.closeServerOnMaxConnections.listenBelow
179188
) {
180189
// The most likely case of error is if the port taken by this application is binded by
181190
// another process during the time the server if closed. In that case there's not much
182191
// we can do. netListen() will be called again every time a connection is dropped, which
183192
// acts as an eventual retry mechanism. onListenError allows the consumer act on this.
184193
this.netListen().catch(e => {
185194
log.error('error attempting to listen server once connection count under limit', e)
186-
this.context.limitServerConnections?.onListenError?.(e as Error)
195+
this.context.closeServerOnMaxConnections?.onListenError?.(e as Error)
187196
})
188197
}
189198
})
@@ -193,8 +202,8 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
193202
}
194203

195204
if (
196-
this.context.limitServerConnections != null &&
197-
this.connections.size >= this.context.limitServerConnections.rejectAbove
205+
this.context.closeServerOnMaxConnections != null &&
206+
this.connections.size >= this.context.closeServerOnMaxConnections.closeAbove
198207
) {
199208
this.netClose()
200209
}

0 commit comments

Comments
 (0)