Skip to content

Commit

Permalink
http: make TCP noDelay enabled by default
Browse files Browse the repository at this point in the history
PR-URL: #42163
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
ShogunPanda authored Mar 9, 2022
1 parent 6b004f1 commit eacd456
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,13 @@ Found'`.
<!-- YAML
added: v0.1.13
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/42163
description: The `noDelay` option now defaults to `true`.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41310
description: The `noDelay`, `keepAlive` and `keepAliveInitialDelay`
options are supported now.
- version:
- v13.8.0
- v12.15.0
Expand Down Expand Up @@ -2871,7 +2878,7 @@ changes:
**Default:** 16384 (16 KB).
* `noDelay` {boolean} If set to `true`, it disables the use of Nagle's
algorithm immediately after a new incoming connection is received.
**Default:** `false`.
**Default:** `true`.
* `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality
on the socket immediately after a new incoming connection is received,
similarly on what is done in \[`socket.setKeepAlive([enable][, initialDelay])`]\[`socket.setKeepAlive(enable, initialDelay)`].
Expand Down
4 changes: 4 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ behavior.
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41310
description: The `noDelay`, `keepAlive` and `keepAliveInitialDelay`
options are supported now.
- version: v12.10.0
pr-url: https://github.com/nodejs/node/pull/25436
description: Added `onread` option.
Expand Down
3 changes: 3 additions & 0 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function Agent(options) {

this.options = { __proto__: null, ...options };

if (this.options.noDelay === undefined)
this.options.noDelay = true;

// Don't confuse net and make it think that we're connecting to a pipe
this.options.path = null;
this.requests = ObjectCreate(null);
Expand Down
3 changes: 3 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ function storeHTTPOptions(options) {
if (insecureHTTPParser !== undefined)
validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser');
this.insecureHTTPParser = insecureHTTPParser;

if (options.noDelay === undefined)
options.noDelay = true;
}

function Server(options, requestListener) {
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-http-nodelay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

const originalConnect = net.Socket.prototype.connect;

net.Socket.prototype.connect = common.mustCall(function(args) {
assert.strictEqual(args[0].noDelay, true);
return originalConnect.call(this, args);
});

const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200);
res.end();
server.close();
}));

server.listen(0, common.mustCall(() => {
assert.strictEqual(server.noDelay, true);

const req = http.request({
method: 'GET',
port: server.address().port
}, common.mustCall((res) => {
res.on('end', () => {
server.close();
res.req.socket.end();
});

res.resume();
}));

req.end();
}));

0 comments on commit eacd456

Please sign in to comment.