Skip to content

Commit

Permalink
net: add drop event for net server
Browse files Browse the repository at this point in the history
PR-URL: #43582
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
theanarkh authored and targos committed Jul 12, 2022
1 parent c4d88b3 commit a0e7b99
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ added: v0.1.90

Emitted when the server has been bound after calling [`server.listen()`][].

### Event: `'drop'`

<!-- YAML
added: REPLACEME
-->

When the number of connections reaches the threshold of `server.maxConnections`,
the server will drop new connections and emit `'drop'` event instead. If it is a
TCP server, the argument is as follows, otherwise the argument is `undefined`.

* `data` {Object} The argument passed to event listener.
* `localAddress` {string} Local address.
* `localPort` {number} Local port.
* `remoteAddress` {string} Remote address.
* `remotePort` {number} Remote port.
* `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.

### `server.address()`

<!-- YAML
Expand Down
20 changes: 20 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol,
ObjectCreate,
} = primordials;

const EventEmitter = require('events');
Expand Down Expand Up @@ -1656,6 +1657,25 @@ function onconnection(err, clientHandle) {
}

if (self.maxConnections && self._connections >= self.maxConnections) {
if (clientHandle.getsockname || clientHandle.getpeername) {
const data = ObjectCreate(null);
if (clientHandle.getsockname) {
const localInfo = ObjectCreate(null);
clientHandle.getsockname(localInfo);
data.localAddress = localInfo.address;
data.localPort = localInfo.port;
}
if (clientHandle.getpeername) {
const remoteInfo = ObjectCreate(null);
clientHandle.getpeername(remoteInfo);
data.remoteAddress = remoteInfo.address;
data.remotePort = remoteInfo.port;
data.remoteFamily = remoteInfo.family;
}
self.emit('drop', data);
} else {
self.emit('drop');
}
clientHandle.close();
return;
}
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-net-server-drop-connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

let firstSocket;
const server = net.createServer(common.mustCall((socket) => {
firstSocket = socket;
}));

server.maxConnections = 1;

server.on('drop', common.mustCall((data) => {
assert.strictEqual(!!data.localAddress, true);
assert.strictEqual(!!data.localPort, true);
assert.strictEqual(!!data.remoteAddress, true);
assert.strictEqual(!!data.remotePort, true);
assert.strictEqual(!!data.remoteFamily, true);
firstSocket.destroy();
server.close();
}));

server.listen(0, () => {
net.createConnection(server.address().port);
net.createConnection(server.address().port);
});

0 comments on commit a0e7b99

Please sign in to comment.