mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
net: add drop event for net server
PR-URL: https://github.com/nodejs/node/pull/43582 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
20
lib/net.js
20
lib/net.js
@@ -31,6 +31,7 @@ const {
|
||||
ObjectDefineProperty,
|
||||
ObjectSetPrototypeOf,
|
||||
Symbol,
|
||||
ObjectCreate,
|
||||
} = primordials;
|
||||
|
||||
const EventEmitter = require('events');
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
26
test/parallel/test-net-server-drop-connections.js
Normal file
26
test/parallel/test-net-server-drop-connections.js
Normal file
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user