File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -281,6 +281,26 @@ added: v0.1.90
281281
282282Emitted when the server has been bound after calling [ ` server.listen() ` ] [ ] .
283283
284+ ### Event: ` 'drop' `
285+
286+ <!-- YAML
287+ added: REPLACEME
288+ -->
289+
290+ When the number of connections reaches the threshold of ` server.maxConnections ` ,
291+ the server will drop new connections and emit ` 'drop' ` event instead. If it is a
292+ TCP server, the arguments is as follow, otherwise return ` undefined ` .
293+
294+ ``` json
295+ {
296+ "localAddress" : " ::ffff:127.0.0.1" ,
297+ "localPort" : 61054 ,
298+ "remoteAddress" : " ::ffff:127.0.0.1" ,
299+ "remotePort" : 61060 ,
300+ "remoteFamily" : " IPv6"
301+ }
302+ ```
303+
284304### ` server.address() `
285305
286306<!-- YAML
Original file line number Diff line number Diff line change @@ -1647,6 +1647,25 @@ function onconnection(err, clientHandle) {
16471647 }
16481648
16491649 if ( self . maxConnections && self . _connections >= self . maxConnections ) {
1650+ if ( clientHandle . getsockname || clientHandle . getpeername ) {
1651+ const data = { } ;
1652+ if ( clientHandle . getsockname ) {
1653+ const localInfo = { } ;
1654+ clientHandle . getsockname ( localInfo ) ;
1655+ data . localAddress = localInfo . address ;
1656+ data . localPort = localInfo . port ;
1657+ }
1658+ if ( clientHandle . getpeername ) {
1659+ const remoteInfo = { } ;
1660+ clientHandle . getpeername ( remoteInfo ) ;
1661+ data . remoteAddress = remoteInfo . address ;
1662+ data . remotePort = remoteInfo . port ;
1663+ data . remoteFamily = remoteInfo . family ;
1664+ }
1665+ self . emit ( 'drop' , data ) ;
1666+ } else {
1667+ self . emit ( 'drop' ) ;
1668+ }
16501669 clientHandle . close ( ) ;
16511670 return ;
16521671 }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const common = require ( '../common' ) ;
3+ const assert = require ( 'assert' ) ;
4+ const net = require ( 'net' ) ;
5+
6+ let firstSocket ;
7+ const server = net . createServer ( common . mustCall ( ( socket ) => {
8+ firstSocket = socket ;
9+ } ) ) ;
10+
11+ server . maxConnections = 1 ;
12+
13+ server . on ( 'drop' , common . mustCall ( ( data ) => {
14+ assert . strictEqual ( ! ! data . localAddress , true ) ;
15+ assert . strictEqual ( ! ! data . localPort , true ) ;
16+ assert . strictEqual ( ! ! data . remoteAddress , true ) ;
17+ assert . strictEqual ( ! ! data . remotePort , true ) ;
18+ assert . strictEqual ( ! ! data . remoteFamily , true ) ;
19+ firstSocket . destroy ( ) ;
20+ server . close ( ) ;
21+ } ) ) ;
22+
23+ server . listen ( 0 , ( ) => {
24+ net . createConnection ( server . address ( ) . port ) ;
25+ net . createConnection ( server . address ( ) . port ) ;
26+ } ) ;
You can’t perform that action at this time.
0 commit comments