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
Expand file treeCollapse file tree
Open diff view settings
Collapse file
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 number | Diff line number | Diff line change |
|---|
@@ -257,6 +257,23 @@ added: v0.1.90 |
257 | 257 |
|
258 | 258 | Emitted when the server has been bound after calling [`server.listen()`][]. |
259 | 259 |
|
| 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 | + |
260 | 277 | ### `server.address()` |
261 | 278 |
|
262 | 279 | <!-- YAML |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/net.js +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ const { |
31 | 31 | ObjectDefineProperty, |
32 | 32 | ObjectSetPrototypeOf, |
33 | 33 | Symbol, |
| 34 | + ObjectCreate, |
34 | 35 | } = primordials; |
35 | 36 |
|
36 | 37 | const EventEmitter = require('events'); |
@@ -1656,6 +1657,25 @@ function onconnection(err, clientHandle) { |
1656 | 1657 | } |
1657 | 1658 |
|
1658 | 1659 | 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 | + } |
1659 | 1679 | clientHandle.close(); |
1660 | 1680 | return; |
1661 | 1681 | } |
|
Collapse file
Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +}); |