Skip to content

Commit

Permalink
http: make idle http parser count configurable
Browse files Browse the repository at this point in the history
PR-URL: #43974
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
  • Loading branch information
theanarkh authored and targos committed Sep 16, 2022
1 parent 6a9e332 commit 9ed48e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
10 changes: 10 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3557,6 +3557,16 @@ try {
}
```

## `http.setMaxIdleHTTPParsers`

<!-- YAML
added: REPLACEME
-->

* {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
Expand Down
9 changes: 7 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -123,7 +124,11 @@ module.exports = {
validateHeaderName,
validateHeaderValue,
get,
request
request,
setMaxIdleHTTPParsers(max) {
validateInteger(max, 'max', 1);
parsers.max = max;
}
};

ObjectDefineProperty(module.exports, 'maxHeaderSize', {
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-http-set-max-idle-http-parser.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 9ed48e3

Please sign in to comment.