Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add maxTotalSockets to agent class #33617

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
NumberIsInteger,
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
Expand Down Expand Up @@ -99,6 +100,10 @@ function Agent(options) {

if (this.maxTotalSockets !== undefined) {
validateNumber(this.maxTotalSockets, 'maxTotalSockets');
if (!NumberIsInteger(this.maxTotalSockets)) {
rickyes marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_OUT_OF_RANGE('maxTotalSockets', 'an integer',
this.maxTotalSockets);
}
if (this.maxTotalSockets <= 0)
rickyes marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_OUT_OF_RANGE('maxTotalSockets', '> 0',
this.maxTotalSockets);
Expand Down Expand Up @@ -262,7 +267,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
this.sockets[name].push(socket);
this.totalSocketCount++;
} else if (sockLen < this.maxSockets &&
this.totalSocketCount < this.maxTotalSockets) {
this.totalSocketCount < this.maxTotalSockets) {
debug('call onSocket', sockLen, freeLen);
// If we are under maxSockets create a new one.
this.createSocket(req, options, (err, socket) => {
Expand Down
30 changes: 21 additions & 9 deletions test/parallel/test-http-agent-maxtotalsockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ assert.throws(() => new http.Agent({
"Received type string ('test')",
});

assert.throws(() => new http.Agent({
maxTotalSockets: -1,
}), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "maxTotalSockets" is out of range. ' +
'It must be > 0. Received -1',
[NaN, Infinity].forEach((item) => {
assert.throws(() => new http.Agent({
maxTotalSockets: item,
}), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "maxTotalSockets" is out of range. ' +
`It must be an integer. Received ${item}`,
});
});

[-1, 0].forEach((item) => {
assert.throws(() => new http.Agent({
maxTotalSockets: item,
}), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "maxTotalSockets" is out of range. ' +
`It must be > 0. Received ${item}`,
});
});

const maxTotalSockets = 2;
Expand Down Expand Up @@ -56,7 +69,7 @@ function handler(s) {
http.get({
host: 'localhost',
port: s.address().port,
agent: agent,
agent,
path: `/${i}`,
}, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
Expand Down Expand Up @@ -91,4 +104,3 @@ function getRequestCount() {

server.listen(0, common.mustCall(() => handler(server)));
server2.listen(0, common.mustCall(() => handler(server2)));