| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2857,6 +2857,16 @@ changes: | |||
| 2857 | 2857 | [`--max-http-header-size`][] for requests received by this server, i.e. | |
| 2858 | 2858 | the maximum length of request headers in bytes. | |
| 2859 | 2859 | **Default:** 16384 (16 KB). | |
| 2860 | + * `noDelay` {boolean} If set to `true`, it disables the use of Nagle's | ||
| 2861 | + algorithm immediately after a new incoming connection is received. | ||
| 2862 | + **Default:** `false`. | ||
| 2863 | + * `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality | ||
| 2864 | + on the socket immediately after a new incoming connection is received, | ||
| 2865 | + similarly on what is done in \[`socket.setKeepAlive([enable][, initialDelay])`]\[`socket.setKeepAlive(enable, initialDelay)`]. | ||
| 2866 | + **Default:** `false`. | ||
| 2867 | + * `keepAliveInitialDelay` {number} If set to a positive number, it sets the | ||
| 2868 | + initial delay before the first keepalive probe is sent on an idle socket. | ||
| 2869 | + **Default:** `0`. | ||
| 2860 | 2870 | ||
| 2861 | 2871 | * `requestListener` {Function} | |
| 2862 | 2872 | ||
@@ -3100,6 +3110,8 @@ changes: | |||
| 3100 | 3110 | * `callback` {Function} | |
| 3101 | 3111 | * Returns: {http.ClientRequest} | |
| 3102 | 3112 | ||
| 3113 | + `options` in [`socket.connect()`][] are also supported. | ||
| 3114 | + | ||
| 3103 | 3115 | Node.js maintains several connections per server to make HTTP requests. | |
| 3104 | 3116 | This function allows one to transparently issue requests. | |
| 3105 | 3117 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -854,6 +854,14 @@ For TCP connections, available `options` are: | |||
| 854 | 854 | `0` indicates that both IPv4 and IPv6 addresses are allowed. **Default:** `0`. | |
| 855 | 855 | * `hints` {number} Optional [`dns.lookup()` hints][]. | |
| 856 | 856 | * `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][]. | |
| 857 | + * `noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm immediately | ||
| 858 | + after the socket is established. **Default:** `false`. | ||
| 859 | + * `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on the socket | ||
| 860 | + immediately after the connection is established, similarly on what is done in | ||
| 861 | + [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`]. | ||
| 862 | + **Default:** `false`. | ||
| 863 | + * `keepAliveInitialDelay` {number} If set to a positive number, it sets the initial delay before | ||
| 864 | + the first keepalive probe is sent on an idle socket.**Default:** `0`. | ||
| 857 | 865 | ||
| 858 | 866 | For [IPC][] connections, available `options` are: | |
| 859 | 867 | ||
@@ -1409,8 +1417,18 @@ added: v0.5.0 | |||
| 1409 | 1417 | **Default:** `false`. | |
| 1410 | 1418 | * `pauseOnConnect` {boolean} Indicates whether the socket should be | |
| 1411 | 1419 | paused on incoming connections. **Default:** `false`. | |
| 1420 | + * `noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm immediately | ||
| 1421 | + after a new incoming connection is received. **Default:** `false`. | ||
| 1422 | + * `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on the socket | ||
| 1423 | + immediately after a new incoming connection is received, similarly on what is done in | ||
| 1424 | + [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`]. | ||
| 1425 | + **Default:** `false`. | ||
| 1426 | + * `keepAliveInitialDelay` {number} If set to a positive number, it sets the initial delay before | ||
| 1427 | + the first keepalive probe is sent on an idle socket.**Default:** `0`. | ||
| 1428 | + | ||
| 1412 | 1429 | * `connectionListener` {Function} Automatically set as a listener for the | |
| 1413 | 1430 | [`'connection'`][] event. | |
| 1431 | + | ||
| 1414 | 1432 | * Returns: {net.Server} | |
| 1415 | 1433 | ||
| 1416 | 1434 | Creates a new TCP or [IPC][] server. | |
@@ -1576,6 +1594,7 @@ net.isIPv6('fhqwhgads'); // returns false | |||
| 1576 | 1594 | [`socket.pause()`]: #socketpause | |
| 1577 | 1595 | [`socket.resume()`]: #socketresume | |
| 1578 | 1596 | [`socket.setEncoding()`]: #socketsetencodingencoding | |
| 1597 | + [`socket.setKeepAlive(enable, initialDelay)`]: #socketsetkeepaliveenable-initialdelay | ||
| 1579 | 1598 | [`socket.setTimeout()`]: #socketsettimeouttimeout-callback | |
| 1580 | 1599 | [`socket.setTimeout(timeout)`]: #socketsettimeouttimeout-callback | |
| 1581 | 1600 | [`writable.destroy()`]: stream.md#writabledestroyerror | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -378,7 +378,11 @@ function Server(options, requestListener) { | |||
| 378 | 378 | } | |
| 379 | 379 | ||
| 380 | 380 | storeHTTPOptions.call(this, options); | |
| 381 | - net.Server.call(this, { allowHalfOpen: true }); | ||
| 381 | + net.Server.call( | ||
| 382 | + this, | ||
| 383 | + { allowHalfOpen: true, noDelay: options.noDelay, | ||
| 384 | + keepAlive: options.keepAlive, | ||
| 385 | + keepAliveInitialDelay: options.keepAliveInitialDelay }); | ||
| 382 | 386 | ||
| 383 | 387 | if (requestListener) { | |
| 384 | 388 | this.on('request', requestListener); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -279,6 +279,8 @@ function initSocketHandle(self) { | |||
| 279 | 279 | const kBytesRead = Symbol('kBytesRead'); | |
| 280 | 280 | const kBytesWritten = Symbol('kBytesWritten'); | |
| 281 | 281 | const kSetNoDelay = Symbol('kSetNoDelay'); | |
| 282 | + const kSetKeepAlive = Symbol('kSetKeepAlive'); | ||
| 283 | + const kSetKeepAliveInitialDelay = Symbol('kSetKeepAliveInitialDelay'); | ||
| 282 | 284 | ||
| 283 | 285 | function Socket(options) { | |
| 284 | 286 | if (!(this instanceof Socket)) return new Socket(options); | |
@@ -297,6 +299,15 @@ function Socket(options) { | |||
| 297 | 299 | 'is not supported' | |
| 298 | 300 | ); | |
| 299 | 301 | } | |
| 302 | + if (typeof options?.keepAliveInitialDelay !== 'undefined') { | ||
| 303 | + validateNumber( | ||
| 304 | + options?.keepAliveInitialDelay, 'options.keepAliveInitialDelay' | ||
| 305 | + ); | ||
| 306 | + | ||
| 307 | + if (options.keepAliveInitialDelay < 0) { | ||
| 308 | + options.keepAliveInitialDelay = 0; | ||
| 309 | + } | ||
| 310 | + } | ||
| 300 | 311 | ||
| 301 | 312 | this.connecting = false; | |
| 302 | 313 | // Problem with this is that users can supply their own handle, that may not | |
@@ -307,7 +318,6 @@ function Socket(options) { | |||
| 307 | 318 | this[kHandle] = null; | |
| 308 | 319 | this._parent = null; | |
| 309 | 320 | this._host = null; | |
| 310 | - this[kSetNoDelay] = false; | ||
| 311 | 321 | this[kLastWriteQueueSize] = 0; | |
| 312 | 322 | this[kTimeout] = null; | |
| 313 | 323 | this[kBuffer] = null; | |
@@ -381,6 +391,10 @@ function Socket(options) { | |||
| 381 | 391 | this[kBufferCb] = onread.callback; | |
| 382 | 392 | } | |
| 383 | 393 | ||
| 394 | + this[kSetNoDelay] = Boolean(options.noDelay); | ||
| 395 | + this[kSetKeepAlive] = Boolean(options.keepAlive); | ||
| 396 | + this[kSetKeepAliveInitialDelay] = ~~(options.keepAliveInitialDelay / 1000); | ||
| 397 | + | ||
| 384 | 398 | // Shut down the socket when we're finished with it. | |
| 385 | 399 | this.on('end', onReadableStreamEnd); | |
| 386 | 400 | ||
@@ -504,31 +518,38 @@ Socket.prototype._onTimeout = function() { | |||
| 504 | 518 | ||
| 505 | 519 | ||
| 506 | 520 | Socket.prototype.setNoDelay = function(enable) { | |
| 521 | + // Backwards compatibility: assume true when `enable` is omitted | ||
| 522 | + enable = Boolean(enable === undefined ? true : enable); | ||
| 523 | + | ||
| 507 | 524 | if (!this._handle) { | |
| 508 | - this.once('connect', | ||
| 509 | - enable ? this.setNoDelay : () => this.setNoDelay(enable)); | ||
| 525 | + this[kSetNoDelay] = enable; | ||
| 510 | 526 | return this; | |
| 511 | 527 | } | |
| 512 | 528 | ||
| 513 | - // Backwards compatibility: assume true when `enable` is omitted | ||
| 514 | - const newValue = enable === undefined ? true : !!enable; | ||
| 515 | - if (this._handle.setNoDelay && newValue !== this[kSetNoDelay]) { | ||
| 516 | - this[kSetNoDelay] = newValue; | ||
| 517 | - this._handle.setNoDelay(newValue); | ||
| 529 | + if (this._handle.setNoDelay && enable !== this[kSetNoDelay]) { | ||
| 530 | + this[kSetNoDelay] = enable; | ||
| 531 | + this._handle.setNoDelay(enable); | ||
| 518 | 532 | } | |
| 519 | 533 | ||
| 520 | 534 | return this; | |
| 521 | 535 | }; | |
| 522 | 536 | ||
| 523 | 537 | ||
| 524 | - Socket.prototype.setKeepAlive = function(setting, msecs) { | ||
| 538 | + Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) { | ||
| 539 | + enable = Boolean(enable); | ||
| 540 | + const initialDelay = ~~(initialDelayMsecs / 1000); | ||
| 541 | + | ||
| 525 | 542 | if (!this._handle) { | |
| 526 | - this.once('connect', () => this.setKeepAlive(setting, msecs)); | ||
| 543 | + this[kSetKeepAlive] = enable; | ||
| 544 | + this[kSetKeepAliveInitialDelay] = initialDelay; | ||
| 527 | 545 | return this; | |
| 528 | 546 | } | |
| 529 | 547 | ||
| 530 | - if (this._handle.setKeepAlive) | ||
| 531 | - this._handle.setKeepAlive(setting, ~~(msecs / 1000)); | ||
| 548 | + if (this._handle.setKeepAlive && enable !== this[kSetKeepAlive]) { | ||
| 549 | + this[kSetKeepAlive] = enable; | ||
| 550 | + this[kSetKeepAliveInitialDelay] = initialDelay; | ||
| 551 | + this._handle.setKeepAlive(enable, initialDelay); | ||
| 552 | + } | ||
| 532 | 553 | ||
| 533 | 554 | return this; | |
| 534 | 555 | }; | |
@@ -1141,6 +1162,14 @@ function afterConnect(status, handle, req, readable, writable) { | |||
| 1141 | 1162 | } | |
| 1142 | 1163 | self._unrefTimer(); | |
| 1143 | 1164 | ||
| 1165 | + if (self[kSetNoDelay] && self._handle.setNoDelay) { | ||
| 1166 | + self._handle.setNoDelay(true); | ||
| 1167 | + } | ||
| 1168 | + | ||
| 1169 | + if (self[kSetKeepAlive] && self._handle.setKeepAlive) { | ||
| 1170 | + self._handle.setKeepAlive(true, self[kSetKeepAliveInitialDelay]); | ||
| 1171 | + } | ||
| 1172 | + | ||
| 1144 | 1173 | self.emit('connect'); | |
| 1145 | 1174 | self.emit('ready'); | |
| 1146 | 1175 | ||
@@ -1204,6 +1233,15 @@ function Server(options, connectionListener) { | |||
| 1204 | 1233 | } else { | |
| 1205 | 1234 | throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); | |
| 1206 | 1235 | } | |
| 1236 | + if (typeof options.keepAliveInitialDelay !== 'undefined') { | ||
| 1237 | + validateNumber( | ||
| 1238 | + options.keepAliveInitialDelay, 'options.keepAliveInitialDelay' | ||
| 1239 | + ); | ||
| 1240 | + | ||
| 1241 | + if (options.keepAliveInitialDelay < 0) { | ||
| 1242 | + options.keepAliveInitialDelay = 0; | ||
| 1243 | + } | ||
| 1244 | + } | ||
| 1207 | 1245 | ||
| 1208 | 1246 | this._connections = 0; | |
| 1209 | 1247 | ||
@@ -1215,6 +1253,9 @@ function Server(options, connectionListener) { | |||
| 1215 | 1253 | ||
| 1216 | 1254 | this.allowHalfOpen = options.allowHalfOpen || false; | |
| 1217 | 1255 | this.pauseOnConnect = !!options.pauseOnConnect; | |
| 1256 | + this.noDelay = Boolean(options.noDelay); | ||
| 1257 | + this.keepAlive = Boolean(options.keepAlive); | ||
| 1258 | + this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000); | ||
| 1218 | 1259 | } | |
| 1219 | 1260 | ObjectSetPrototypeOf(Server.prototype, EventEmitter.prototype); | |
| 1220 | 1261 | ObjectSetPrototypeOf(Server, EventEmitter); | |
@@ -1567,6 +1608,14 @@ function onconnection(err, clientHandle) { | |||
| 1567 | 1608 | writable: true | |
| 1568 | 1609 | }); | |
| 1569 | 1610 | ||
| 1611 | + if (self.noDelay && handle.setNoDelay) { | ||
| 1612 | + handle.setNoDelay(true); | ||
| 1613 | + } | ||
| 1614 | + | ||
| 1615 | + if (self.keepAlive && self.setKeepAlive) { | ||
| 1616 | + handle.setKeepAlive(true, handle.keepAliveInitialDelay); | ||
| 1617 | + } | ||
| 1618 | + | ||
| 1570 | 1619 | self._connections++; | |
| 1571 | 1620 | socket.server = self; | |
| 1572 | 1621 | socket._server = self; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const net = require('net'); | ||
| 6 | + | ||
| 7 | + const truthyValues = [true, 1, 'true', {}, []]; | ||
| 8 | + const delays = [[123, 0], [456123, 456], [-123000, 0], [undefined, 0]]; | ||
| 9 | + const falseyValues = [false, 0, '']; | ||
| 10 | + | ||
| 11 | + const genSetKeepAlive = (desiredEnable, desiredDelay) => (enable, delay) => { | ||
| 12 | + assert.strictEqual(enable, desiredEnable); | ||
| 13 | + assert.strictEqual(delay, desiredDelay); | ||
| 14 | + }; | ||
| 15 | + | ||
| 16 | + for (const value of truthyValues) { | ||
| 17 | + for (const delay of delays) { | ||
| 18 | + const server = net.createServer(); | ||
| 19 | + | ||
| 20 | + server.listen(0, common.mustCall(function() { | ||
| 21 | + const port = server.address().port; | ||
| 22 | + | ||
| 23 | + const client = net.connect( | ||
| 24 | + { port, keepAlive: value, keepAliveInitialDelay: delay[0] }, | ||
| 25 | + common.mustCall(() => client.end()) | ||
| 26 | + ); | ||
| 27 | + | ||
| 28 | + client._handle.setKeepAlive = common.mustCall( | ||
| 29 | + genSetKeepAlive(true, delay[1]) | ||
| 30 | + ); | ||
| 31 | + | ||
| 32 | + client.on('end', common.mustCall(function() { | ||
| 33 | + server.close(); | ||
| 34 | + })); | ||
| 35 | + })); | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + for (const value of falseyValues) { | ||
| 40 | + const server = net.createServer(); | ||
| 41 | + | ||
| 42 | + server.listen(0, common.mustCall(function() { | ||
| 43 | + const port = server.address().port; | ||
| 44 | + | ||
| 45 | + const client = net.connect( | ||
| 46 | + { port, keepAlive: value }, | ||
| 47 | + common.mustCall(() => client.end()) | ||
| 48 | + ); | ||
| 49 | + | ||
| 50 | + client._handle.setKeepAlive = common.mustNotCall(); | ||
| 51 | + | ||
| 52 | + client.on('end', common.mustCall(function() { | ||
| 53 | + server.close(); | ||
| 54 | + })); | ||
| 55 | + })); | ||
| 56 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,49 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const net = require('net'); | ||
| 6 | + | ||
| 7 | + const truthyValues = [true, 1, 'true', {}, []]; | ||
| 8 | + const falseyValues = [false, 0, '']; | ||
| 9 | + const genSetNoDelay = (desiredArg) => (enable) => { | ||
| 10 | + assert.strictEqual(enable, desiredArg); | ||
| 11 | + }; | ||
| 12 | + | ||
| 13 | + for (const value of truthyValues) { | ||
| 14 | + const server = net.createServer(); | ||
| 15 | + | ||
| 16 | + server.listen(0, common.mustCall(function() { | ||
| 17 | + const port = server.address().port; | ||
| 18 | + | ||
| 19 | + const client = net.connect( | ||
| 20 | + { port, noDelay: value }, | ||
| 21 | + common.mustCall(() => client.end()) | ||
| 22 | + ); | ||
| 23 | + | ||
| 24 | + client._handle.setNoDelay = common.mustCall(genSetNoDelay(true)); | ||
| 25 | + | ||
| 26 | + client.on('end', common.mustCall(function() { | ||
| 27 | + server.close(); | ||
| 28 | + })); | ||
| 29 | + })); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + for (const value of falseyValues) { | ||
| 33 | + const server = net.createServer(); | ||
| 34 | + | ||
| 35 | + server.listen(0, common.mustCall(function() { | ||
| 36 | + const port = server.address().port; | ||
| 37 | + | ||
| 38 | + const client = net.connect( | ||
| 39 | + { port, noDelay: value }, | ||
| 40 | + common.mustCall(() => client.end()) | ||
| 41 | + ); | ||
| 42 | + | ||
| 43 | + client._handle.setNoDelay = common.mustNotCall(); | ||
| 44 | + | ||
| 45 | + client.on('end', common.mustCall(function() { | ||
| 46 | + server.close(); | ||
| 47 | + })); | ||
| 48 | + })); | ||
| 49 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments