From fa6183fc7ef06d5bf645c8f47173ab986b194cde Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 16 Aug 2022 21:09:10 +0800 Subject: [PATCH] http: add max for http keepalive PR-URL: https://github.com/nodejs/node/pull/44217 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina --- lib/_http_outgoing.js | 6 +++++- lib/_http_server.js | 1 + test/parallel/test-http-keep-alive-max-requests.js | 2 +- test/parallel/test-http-keep-alive-pipeline-max-requests.js | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index dcdea29968590a..7b1961ad978580 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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; diff --git a/lib/_http_server.js b/lib/_http_server.js index be444c42120f35..4e23266f630525 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -922,6 +922,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); diff --git a/test/parallel/test-http-keep-alive-max-requests.js b/test/parallel/test-http-keep-alive-max-requests.js index 657b59ae6d9369..89546df5ae8a63 100644 --- a/test/parallel/test-http-keep-alive-max-requests.js +++ b/test/parallel/test-http-keep-alive-max-requests.js @@ -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); } } diff --git a/test/parallel/test-http-keep-alive-pipeline-max-requests.js b/test/parallel/test-http-keep-alive-pipeline-max-requests.js index 9c5d46a57ce197..5549440d3dd5ae 100644 --- a/test/parallel/test-http-keep-alive-pipeline-max-requests.js +++ b/test/parallel/test-http-keep-alive-pipeline-max-requests.js @@ -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); } }