Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: add drop event for net server #43582

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'`

theanarkh marked this conversation as resolved.
Show resolved Hide resolved
<!-- 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 @@ -1647,6 +1648,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);
});