Skip to content

Commit

Permalink
http: make TCP noDelay enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Mar 1, 2022
1 parent b3723fa commit d7016f5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
10 changes: 7 additions & 3 deletions doc/api/http.md
Expand Up @@ -2838,6 +2838,10 @@ Found'`.
<!-- YAML
added: v0.1.13
changes:
- 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,14 +2875,14 @@ 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)`].
**Default:** `false`.
**Default:** `true`.
* `keepAliveInitialDelay` {number} If set to a positive number, it sets the
initial delay before the first keepalive probe is sent on an idle socket.
**Default:** `0`.
**Default:** `1000`.

* `requestListener` {Function}

Expand Down
4 changes: 4 additions & 0 deletions doc/api/net.md
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
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
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
@@ -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 d7016f5

Please sign in to comment.