@@ -27,9 +27,11 @@ async function attemptClose (maConn: MultiaddrConnection) {
27
27
}
28
28
}
29
29
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
33
35
onListenError ?: ( err : Error ) => void
34
36
}
35
37
@@ -40,7 +42,7 @@ interface Context extends TCPCreateListenerOptions {
40
42
socketCloseTimeout ?: number
41
43
maxConnections ?: number
42
44
metrics ?: Metrics
43
- limitServerConnections ?: LimitServerConnectionsOpts
45
+ closeServerOnMaxConnections ?: CloseServerOnMaxConnectionsOpts
44
46
}
45
47
46
48
const SERVER_STATUS_UP = 1
@@ -82,6 +84,13 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
82
84
this . server . maxConnections = context . maxConnections
83
85
}
84
86
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
+
85
94
this . server
86
95
. on ( 'listening' , ( ) => {
87
96
if ( context . metrics != null ) {
@@ -174,16 +183,16 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
174
183
this . connections . delete ( maConn )
175
184
176
185
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
179
188
) {
180
189
// The most likely case of error is if the port taken by this application is binded by
181
190
// another process during the time the server if closed. In that case there's not much
182
191
// we can do. netListen() will be called again every time a connection is dropped, which
183
192
// acts as an eventual retry mechanism. onListenError allows the consumer act on this.
184
193
this . netListen ( ) . catch ( e => {
185
194
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 )
187
196
} )
188
197
}
189
198
} )
@@ -193,8 +202,8 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
193
202
}
194
203
195
204
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
198
207
) {
199
208
this . netClose ( )
200
209
}
0 commit comments