Skip to content

Commit 851264c

Browse files
theanarkhruyadorno
authored andcommitted
http: add max for http keepalive
PR-URL: #44217 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c842ab3 commit 851264c

4 files changed

+9
-4
lines changed

lib/_http_outgoing.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ function _storeHeader(firstLine, headers) {
463463
header += 'Connection: keep-alive\r\n';
464464
if (this._keepAliveTimeout && this._defaultKeepAlive) {
465465
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
466-
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
466+
let max = '';
467+
if (~~this._maxRequestsPerSocket > 0) {
468+
max = `, max=${this._maxRequestsPerSocket}`;
469+
}
470+
header += `Keep-Alive: timeout=${timeoutSeconds}${max}\r\n`;
467471
}
468472
} else {
469473
this._last = true;

lib/_http_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
959959

960960
const res = new server[kServerResponse](req);
961961
res._keepAliveTimeout = server.keepAliveTimeout;
962+
res._maxRequestsPerSocket = server.maxRequestsPerSocket;
962963
res._onPendingData = updateOutgoingData.bind(undefined,
963964
socket, state);
964965

test/parallel/test-http-keep-alive-max-requests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function assertResponse(headers, body, expectClosed) {
1414
assert.match(body, /Hello World!/m);
1515
} else {
1616
assert.match(headers, /Connection: keep-alive\r\n/m);
17-
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
17+
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m);
1818
assert.match(body, /Hello World!/m);
1919
}
2020
}

test/parallel/test-http-keep-alive-pipeline-max-requests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const bodySent = 'This is my request';
1010
function assertResponse(headers, body, expectClosed) {
1111
if (expectClosed) {
1212
assert.match(headers, /Connection: close\r\n/m);
13-
assert.strictEqual(headers.search(/Keep-Alive: timeout=5\r\n/m), -1);
13+
assert.strictEqual(headers.search(/Keep-Alive: timeout=5, max=3\r\n/m), -1);
1414
assert.match(body, /Hello World!/m);
1515
} else {
1616
assert.match(headers, /Connection: keep-alive\r\n/m);
17-
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
17+
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m);
1818
assert.match(body, /Hello World!/m);
1919
}
2020
}

0 commit comments

Comments
 (0)