Skip to content

Commit

Permalink
http: add max for http keepalive
Browse files Browse the repository at this point in the history
PR-URL: #44217
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
theanarkh authored and ruyadorno committed Aug 22, 2022
1 parent c842ab3 commit 851264c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,11 @@ function _storeHeader(firstLine, headers) {
header += 'Connection: keep-alive\r\n';
if (this._keepAliveTimeout && this._defaultKeepAlive) {
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
let max = '';
if (~~this._maxRequestsPerSocket > 0) {
max = `, max=${this._maxRequestsPerSocket}`;
}
header += `Keep-Alive: timeout=${timeoutSeconds}${max}\r\n`;
}
} else {
this._last = true;
Expand Down
1 change: 1 addition & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {

const res = new server[kServerResponse](req);
res._keepAliveTimeout = server.keepAliveTimeout;
res._maxRequestsPerSocket = server.maxRequestsPerSocket;
res._onPendingData = updateOutgoingData.bind(undefined,
socket, state);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-keep-alive-max-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function assertResponse(headers, body, expectClosed) {
assert.match(body, /Hello World!/m);
} else {
assert.match(headers, /Connection: keep-alive\r\n/m);
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m);
assert.match(body, /Hello World!/m);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-keep-alive-pipeline-max-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const bodySent = 'This is my request';
function assertResponse(headers, body, expectClosed) {
if (expectClosed) {
assert.match(headers, /Connection: close\r\n/m);
assert.strictEqual(headers.search(/Keep-Alive: timeout=5\r\n/m), -1);
assert.strictEqual(headers.search(/Keep-Alive: timeout=5, max=3\r\n/m), -1);
assert.match(body, /Hello World!/m);
} else {
assert.match(headers, /Connection: keep-alive\r\n/m);
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m);
assert.match(body, /Hello World!/m);
}
}
Expand Down

0 comments on commit 851264c

Please sign in to comment.