| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -281,6 +281,23 @@ added: v0.1.90 | |||
| 281 | 281 | ||
| 282 | 282 | Emitted when the server has been bound after calling [`server.listen()`][]. | |
| 283 | 283 | ||
| 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 | + | ||
| 284 | 301 | ### `server.address()` | |
| 285 | 302 | ||
| 286 | 303 | <!-- YAML | |
| 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 | } | |
| Original file line number | Diff line number | Diff 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 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments