Skip to content

Commit 6125477

Browse files
committed
net: add drop event for net server
1 parent 42ad967 commit 6125477

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

doc/api/net.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,23 @@ added: v0.1.90
281281

282282
Emitted 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 argument is as follows, otherwise the argument is `undefined`.
293+
294+
* `data` {Object} The argument passed to event listener.
295+
* `localAddress` {string} Local address.
296+
* `localPort` {number} Local port.
297+
* `remoteAddress` {string} Remote address.
298+
* `remotePort` {number} Remote port.
299+
* `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.
300+
284301
### `server.address()`
285302

286303
<!-- YAML

lib/net.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
ObjectDefineProperty,
3232
ObjectSetPrototypeOf,
3333
Symbol,
34+
ObjectCreate,
3435
} = primordials;
3536

3637
const EventEmitter = require('events');
@@ -1647,6 +1648,25 @@ function onconnection(err, clientHandle) {
16471648
}
16481649

16491650
if (self.maxConnections && self._connections >= self.maxConnections) {
1651+
if (clientHandle.getsockname || clientHandle.getpeername) {
1652+
const data = ObjectCreate(null);
1653+
if (clientHandle.getsockname) {
1654+
const localInfo = ObjectCreate(null);
1655+
clientHandle.getsockname(localInfo);
1656+
data.localAddress = localInfo.address;
1657+
data.localPort = localInfo.port;
1658+
}
1659+
if (clientHandle.getpeername) {
1660+
const remoteInfo = ObjectCreate(null);
1661+
clientHandle.getpeername(remoteInfo);
1662+
data.remoteAddress = remoteInfo.address;
1663+
data.remotePort = remoteInfo.port;
1664+
data.remoteFamily = remoteInfo.family;
1665+
}
1666+
self.emit('drop', data);
1667+
} else {
1668+
self.emit('drop');
1669+
}
16501670
clientHandle.close();
16511671
return;
16521672
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
});

0 commit comments

Comments
 (0)