GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — net: add drop event for net server · nodejs/node@303bd08

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • net.md
  • lib
    • net.js
  • test/parallel
    • test-net-server-drop-connections.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/net.md‎

Copy file name to clipboardExpand all lines: doc/api/net.md
+17Lines changed: 17 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,23 @@ added: v0.1.90
257257

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

260+
### Event: `'drop'`
261+
262+
<!-- YAML
263+
added: REPLACEME
264+
-->
265+
266+
When the number of connections reaches the threshold of `server.maxConnections`,
267+
the server will drop new connections and emit `'drop'` event instead. If it is a
268+
TCP server, the argument is as follows, otherwise the argument is `undefined`.
269+
270+
* `data` {Object} The argument passed to event listener.
271+
* `localAddress` {string} Local address.
272+
* `localPort` {number} Local port.
273+
* `remoteAddress` {string} Remote address.
274+
* `remotePort` {number} Remote port.
275+
* `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.
276+
260277
### `server.address()`
261278

262279
<!-- YAML
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+20Lines 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');
@@ -1656,6 +1657,25 @@ function onconnection(err, clientHandle) {
16561657
}
16571658

16581659
if (self.maxConnections && self._connections >= self.maxConnections) {
1660+
if (clientHandle.getsockname || clientHandle.getpeername) {
1661+
const data = ObjectCreate(null);
1662+
if (clientHandle.getsockname) {
1663+
const localInfo = ObjectCreate(null);
1664+
clientHandle.getsockname(localInfo);
1665+
data.localAddress = localInfo.address;
1666+
data.localPort = localInfo.port;
1667+
}
1668+
if (clientHandle.getpeername) {
1669+
const remoteInfo = ObjectCreate(null);
1670+
clientHandle.getpeername(remoteInfo);
1671+
data.remoteAddress = remoteInfo.address;
1672+
data.remotePort = remoteInfo.port;
1673+
data.remoteFamily = remoteInfo.family;
1674+
}
1675+
self.emit('drop', data);
1676+
} else {
1677+
self.emit('drop');
1678+
}
16591679
clientHandle.close();
16601680
return;
16611681
}
Collapse file

‎test/parallel/test-net-server-drop-connections.js‎

Copy file name to clipboard
+26Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.