From 8398e98b1b922db908cc86d5298d0958efe6711e Mon Sep 17 00:00:00 2001 From: theanarkh Date: Fri, 29 Jul 2022 17:00:38 +0800 Subject: [PATCH] http: make idle http parser count configurable PR-URL: https://github.com/nodejs/node/pull/43974 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina Reviewed-By: Feng Yu --- doc/api/http.md | 10 ++++++++++ lib/http.js | 9 +++++++-- .../test-http-set-max-idle-http-parser.js | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-http-set-max-idle-http-parser.js diff --git a/doc/api/http.md b/doc/api/http.md index fdd9688ea3a82b..f81d970fc66583 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -3557,6 +3557,16 @@ try { } ``` +## `http.setMaxIdleHTTPParsers` + + + +* {number} + +Set the maximum number of idle HTTP parsers. **Default:** `1000`. + [RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt [`'checkContinue'`]: #event-checkcontinue [`'finish'`]: #event-finish diff --git a/lib/http.js b/lib/http.js index 1366656e42eb94..d7acafe4f317b8 100644 --- a/lib/http.js +++ b/lib/http.js @@ -27,9 +27,10 @@ const { ObjectDefineProperty, } = primordials; +const { validateInteger } = require('internal/validators'); const httpAgent = require('_http_agent'); const { ClientRequest } = require('_http_client'); -const { methods } = require('_http_common'); +const { methods, parsers } = require('_http_common'); const { IncomingMessage } = require('_http_incoming'); const { validateHeaderName, @@ -123,7 +124,11 @@ module.exports = { validateHeaderName, validateHeaderValue, get, - request + request, + setMaxIdleHTTPParsers(max) { + validateInteger(max, 'max', 1); + parsers.max = max; + } }; ObjectDefineProperty(module.exports, 'maxHeaderSize', { diff --git a/test/parallel/test-http-set-max-idle-http-parser.js b/test/parallel/test-http-set-max-idle-http-parser.js new file mode 100644 index 00000000000000..d935823a1ba946 --- /dev/null +++ b/test/parallel/test-http-set-max-idle-http-parser.js @@ -0,0 +1,19 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const httpCommon = require('_http_common'); +const http = require('http'); + +[Symbol(), {}, [], () => {}, 1n, true, '1', null, undefined].forEach((value) => { + assert.throws(() => http.setMaxIdleHTTPParsers(value), { code: 'ERR_INVALID_ARG_TYPE' }); +}); + +[-1, -Infinity, NaN, 0, 1.1].forEach((value) => { + assert.throws(() => http.setMaxIdleHTTPParsers(value), { code: 'ERR_OUT_OF_RANGE' }); +}); + +[1, Number.MAX_SAFE_INTEGER].forEach((value) => { + assert.notStrictEqual(httpCommon.parsers.max, value); + http.setMaxIdleHTTPParsers(value); + assert.strictEqual(httpCommon.parsers.max, value); +});