mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
http: fix timeout reset after keep-alive timeout
Fix the logic of resetting the socket timeout of keep-alive HTTP connections and add two tests: * `test-http-server-keep-alive-timeout-slow-server` is a regression test for GH-13391. It ensures that the server-side keep-alive timeout will not fire during processing of a request. * `test-http-server-keep-alive-timeout-slow-client-headers` ensures that the regular socket timeout is restored as soon as a client starts sending a new request, not as soon as the whole message is received, so that the keep-alive timeout will not fire while, e.g., the client is sending large cookies. Refs: https://github.com/nodejs/node/pull/2534 Fixes: https://github.com/nodejs/node/issues/13391 PR-URL: https://github.com/nodejs/node/pull/13549 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
1 parent
f23b3b6bad
commit
e9ae4aaaad
3 files changed
+119
-8
No files matched your search
+12
-8
@@ -375,14 +375,6 @@ function connectionListener(socket) {
|
||||
assert(!socket._paused);
|
||||
debug('SERVER socketOnData %d', d.length);
|
||||
|
||||
if (socket._keepAliveTimeoutSet) {
|
||||
socket.setTimeout(0);
|
||||
if (self.timeout) {
|
||||
socket.setTimeout(self.timeout);
|
||||
}
|
||||
socket._keepAliveTimeoutSet = false;
|
||||
}
|
||||
|
||||
var ret = parser.execute(d);
|
||||
|
||||
onParserExecuteCommon(ret, d);
|
||||
@@ -409,6 +401,8 @@ function connectionListener(socket) {
|
||||
}
|
||||
|
||||
function onParserExecuteCommon(ret, d) {
|
||||
resetSocketTimeout(self, socket);
|
||||
|
||||
if (ret instanceof Error) {
|
||||
debug('parse error', ret);
|
||||
socketOnError.call(socket, ret);
|
||||
@@ -491,6 +485,8 @@ function connectionListener(socket) {
|
||||
}
|
||||
|
||||
function parserOnIncoming(req, shouldKeepAlive) {
|
||||
resetSocketTimeout(self, socket);
|
||||
|
||||
incoming.push(req);
|
||||
|
||||
// Set to zero to communicate that we have finished parsing.
|
||||
@@ -589,6 +585,14 @@ function connectionListener(socket) {
|
||||
}
|
||||
exports._connectionListener = connectionListener;
|
||||
|
||||
function resetSocketTimeout(server, socket) {
|
||||
if (!socket._keepAliveTimeoutSet)
|
||||
return;
|
||||
|
||||
socket.setTimeout(server.timeout || 0);
|
||||
socket._keepAliveTimeoutSet = false;
|
||||
}
|
||||
|
||||
function onSocketResume() {
|
||||
// It may seem that the socket is resumed, but this is an enemy's trick to
|
||||
// deceive us! `resume` is emitted asynchronously, and may be called from
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const http = require('http');
|
||||
const net = require('net');
|
||||
|
||||
const server = http.createServer(common.mustCall((req, res) => {
|
||||
res.end();
|
||||
}, 2));
|
||||
|
||||
server.keepAliveTimeout = common.platformTimeout(100);
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const port = server.address().port;
|
||||
const socket = net.connect({ port }, common.mustCall(() => {
|
||||
request(common.mustCall(() => {
|
||||
// Make a second request on the same socket, after the keep-alive timeout
|
||||
// has been set on the server side.
|
||||
request(common.mustCall());
|
||||
}));
|
||||
}));
|
||||
|
||||
server.on('timeout', common.mustCall(() => {
|
||||
socket.end();
|
||||
server.close();
|
||||
}));
|
||||
|
||||
function request(callback) {
|
||||
socket.setEncoding('utf8');
|
||||
socket.on('data', onData);
|
||||
let response = '';
|
||||
|
||||
// Simulate a client that sends headers slowly (with a period of inactivity
|
||||
// that is longer than the keep-alive timeout).
|
||||
socket.write('GET / HTTP/1.1\r\n' +
|
||||
`Host: localhost:${port}\r\n`);
|
||||
setTimeout(() => {
|
||||
socket.write('Connection: keep-alive\r\n' +
|
||||
'\r\n');
|
||||
}, common.platformTimeout(300));
|
||||
|
||||
function onData(chunk) {
|
||||
response += chunk;
|
||||
if (chunk.includes('\r\n')) {
|
||||
socket.removeListener('data', onData);
|
||||
onHeaders();
|
||||
}
|
||||
}
|
||||
|
||||
function onHeaders() {
|
||||
assert.ok(response.includes('HTTP/1.1 200 OK\r\n'));
|
||||
assert.ok(response.includes('Connection: keep-alive\r\n'));
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}));
|
||||
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const http = require('http');
|
||||
|
||||
const server = http.createServer(common.mustCall((req, res) => {
|
||||
if (req.url === '/first') {
|
||||
res.end('ok');
|
||||
return;
|
||||
}
|
||||
setTimeout(() => {
|
||||
res.end('ok');
|
||||
}, common.platformTimeout(500));
|
||||
}, 2));
|
||||
|
||||
server.keepAliveTimeout = common.platformTimeout(200);
|
||||
|
||||
const agent = new http.Agent({
|
||||
keepAlive: true,
|
||||
maxSockets: 1
|
||||
});
|
||||
|
||||
function request(path, callback) {
|
||||
const port = server.address().port;
|
||||
const req = http.request({ agent, path, port }, common.mustCall((res) => {
|
||||
assert.strictEqual(res.statusCode, 200);
|
||||
|
||||
res.setEncoding('utf8');
|
||||
|
||||
let result = '';
|
||||
res.on('data', (chunk) => {
|
||||
result += chunk;
|
||||
});
|
||||
|
||||
res.on('end', common.mustCall(() => {
|
||||
assert.strictEqual(result, 'ok');
|
||||
callback();
|
||||
}));
|
||||
}));
|
||||
req.end();
|
||||
}
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
request('/first', () => {
|
||||
request('/second', () => {
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
}));
|
||||
Reference in new issue
Block a user