Skip to content

Commit e2dba6d

Browse files
committed
net: make TCP keep-alive and TCP noDelay enabled by default
1 parent b3723fa commit e2dba6d

29 files changed

+121
-30
lines changed

doc/api/http.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ changes:
144144
header is always sent when using an agent except when the `Connection`
145145
header is explicitly specified or when the `keepAlive` and `maxSockets`
146146
options are respectively set to `false` and `Infinity`, in which case
147-
`Connection: close` will be used. **Default:** `false`.
147+
`Connection: close` will be used. **Default:** `true`.
148148
* `keepAliveMsecs` {number} When using the `keepAlive` option, specifies
149149
the [initial delay][]
150150
for TCP Keep-Alive packets. Ignored when the
151-
`keepAlive` option is `false` or `undefined`. **Default:** `1000`.
151+
`keepAlive` option is `false`. **Default:** `1000`.
152152
* `maxSockets` {number} Maximum number of sockets to allow per host.
153153
If the same host opens multiple concurrent connections, each request
154154
will use new socket until the `maxSockets` value is reached.
@@ -2871,14 +2871,14 @@ changes:
28712871
**Default:** 16384 (16 KB).
28722872
* `noDelay` {boolean} If set to `true`, it disables the use of Nagle's
28732873
algorithm immediately after a new incoming connection is received.
2874-
**Default:** `false`.
2874+
**Default:** `true`.
28752875
* `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality
28762876
on the socket immediately after a new incoming connection is received,
28772877
similarly on what is done in \[`socket.setKeepAlive([enable][, initialDelay])`]\[`socket.setKeepAlive(enable, initialDelay)`].
2878-
**Default:** `false`.
2878+
**Default:** `true`.
28792879
* `keepAliveInitialDelay` {number} If set to a positive number, it sets the
28802880
initial delay before the first keepalive probe is sent on an idle socket.
2881-
**Default:** `0`.
2881+
**Default:** `60000`.
28822882

28832883
* `requestListener` {Function}
28842884

lib/_http_agent.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ function Agent(options) {
101101

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

104+
if (this.options.noDelay === undefined)
105+
this.options.noDelay = true;
106+
if (this.options.keepAlive === undefined)
107+
this.options.keepAlive = true;
108+
if (this.options.keepAliveInitialDelay === undefined)
109+
this.options.keepAliveInitialDelay = 1000;
110+
104111
// Don't confuse net and make it think that we're connecting to a pipe
105112
this.options.path = null;
106113
this.requests = ObjectCreate(null);
107114
this.sockets = ObjectCreate(null);
108115
this.freeSockets = ObjectCreate(null);
109-
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
110-
this.keepAlive = this.options.keepAlive || false;
116+
this.keepAliveMsecs =
117+
this.options.keepAliveMsecs || this.options.keepAliveInitialDelay || 1000;
118+
this.keepAlive = this.options.keepAlive;
111119
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
112120
this.maxFreeSockets = this.options.maxFreeSockets || 256;
113121
this.scheduling = this.options.scheduling || 'lifo';

lib/_http_server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ function storeHTTPOptions(options) {
363363
if (insecureHTTPParser !== undefined)
364364
validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser');
365365
this.insecureHTTPParser = insecureHTTPParser;
366+
367+
if (options.noDelay === undefined)
368+
options.noDelay = true;
369+
if (options.keepAlive === undefined)
370+
options.keepAlive = true;
371+
if (options.keepAliveInitialDelay === undefined)
372+
options.keepAliveInitialDelay = 1000;
366373
}
367374

368375
function Server(options, requestListener) {

test/async-hooks/test-graph.http.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ server.listen(0, common.mustCall(() => {
1919
http.get({
2020
host: '::1',
2121
family: 6,
22-
port: server.address().port
23-
}, common.mustCall());
22+
port: server.address().port,
23+
headers: { connection: 'close' }
24+
}, common.mustCall(() => {}));
2425
}));
2526

2627
process.on('exit', () => {

test/parallel/test-child-process-http-socket-leak.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ server.listen(0, common.mustCall(() => {
4949
assert.strictEqual(socket[kTimeout], null);
5050
assert.strictEqual(socket.parser, null);
5151
assert.strictEqual(socket._httpMessage, null);
52+
res.req.socket.end();
5253
}));
5354
}));
5455

test/parallel/test-http-client-agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ const max = 3;
3131
const server = http.Server(common.mustCall((req, res) => {
3232
if (req.url === '/0') {
3333
setTimeout(common.mustCall(() => {
34-
res.writeHead(200);
34+
res.writeHead(200, { connection: 'close' });
3535
res.end('Hello, World!');
3636
}), 100);
3737
} else {
38-
res.writeHead(200);
38+
res.writeHead(200, { connection: 'close' });
3939
res.end('Hello, World!');
4040
}
4141
}, max));

test/parallel/test-http-client-headers-array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function execute(options) {
1010
const expectHeaders = {
1111
'x-foo': 'boom',
1212
'cookie': 'a=1; b=2; c=3',
13-
'connection': 'close'
13+
'connection': 'keep-alive'
1414
};
1515

1616
// no Host header when you set headers an array

test/parallel/test-http-client-readable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class FakeAgent extends http.Agent {
5353

5454
return s;
5555
}
56+
57+
keepSocketAlive() {}
5658
}
5759

5860
let received = '';

test/parallel/test-http-client-spurious-aborted.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function download() {
4040
req.on('error', common.mustNotCall());
4141
req.on('response', (res) => {
4242
assert.strictEqual(res.statusCode, 200);
43-
assert.strictEqual(res.headers.connection, 'close');
43+
assert.strictEqual(res.headers.connection, 'keep-alive');
4444
let aborted = false;
4545
const writable = new Writable({
4646
write(chunk, encoding, callback) {

test/parallel/test-http-content-length.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,29 @@ const countdown = new Countdown(3, () => server.close());
2424

2525
const server = http.createServer(function(req, res) {
2626
res.removeHeader('Date');
27+
res.setHeader('connection', 'close');
2728

2829
switch (req.url.substr(1)) {
2930
case 'multiple-writes':
30-
assert.deepStrictEqual(req.headers, expectedHeadersMultipleWrites);
31+
assert.deepStrictEqual(
32+
req.headers,
33+
{ ...expectedHeadersMultipleWrites, 'connection': 'keep-alive' }
34+
);
3135
res.write('hello');
3236
res.end('world');
3337
break;
3438
case 'end-with-data':
35-
assert.deepStrictEqual(req.headers, expectedHeadersEndWithData);
39+
assert.deepStrictEqual(
40+
req.headers,
41+
{ ...expectedHeadersEndWithData, 'connection': 'keep-alive' }
42+
);
3643
res.end('hello world');
3744
break;
3845
case 'empty':
39-
assert.deepStrictEqual(req.headers, expectedHeadersEndNoData);
46+
assert.deepStrictEqual(
47+
req.headers,
48+
{ ...expectedHeadersEndNoData, 'connection': 'keep-alive' }
49+
);
4050
res.end();
4151
break;
4252
default:

0 commit comments

Comments
 (0)