-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add maxTotalSockets to agent class
Add maxTotalSockets to determine how many sockets an agent can open. Unlike maxSockets, The maxTotalSockets does not count by per origin. PR-URL: #33617 Fixes: #31942 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
- v23.5.0
- v23.4.0
- v23.3.0
- v23.2.0
- v23.1.0
- v23.0.0
- v22.12.0
- v22.11.0
- v22.10.0
- v22.9.0
- v22.8.0
- v22.7.0
- v22.6.0
- v22.5.1
- v22.5.0
- v22.4.1
- v22.4.0
- v22.3.0
- v22.2.0
- v22.1.0
- v22.0.0
- v21.7.3
- v21.7.2
- v21.7.1
- v21.7.0
- v21.6.2
- v21.6.1
- v21.6.0
- v21.5.0
- v21.4.0
- v21.3.0
- v21.2.0
- v21.1.0
- v21.0.0
- v20.18.1
- v20.18.0
- v20.17.0
- v20.16.0
- v20.15.1
- v20.15.0
- v20.14.0
- v20.13.1
- v20.13.0
- v20.12.2
- v20.12.1
- v20.12.0
- v20.11.1
- v20.11.0
- v20.10.0
- v20.9.0
- v20.8.1
- v20.8.0
- v20.7.0
- v20.6.1
- v20.6.0
- v20.5.1
- v20.5.0
- v20.4.0
- v20.3.1
- v20.3.0
- v20.2.0
- v20.1.0
- v20.0.0
- v19.9.0
- v19.8.1
- v19.8.0
- v19.7.0
- v19.6.1
- v19.6.0
- v19.5.0
- v19.4.0
- v19.3.0
- v19.2.0
- v19.1.0
- v19.0.1
- v19.0.0
- v18.20.5
- v18.20.4
- v18.20.3
- v18.20.2
- v18.20.1
- v18.20.0
- v18.19.1
- v18.19.0
- v18.18.2
- v18.18.1
- v18.18.0
- v18.17.1
- v18.17.0
- v18.16.1
- v18.16.0
- v18.15.0
- v18.14.2
- v18.14.1
- v18.14.0
- v18.13.0
- v18.12.1
- v18.12.0
- v18.11.0
- v18.10.0
- v18.9.1
- v18.9.0
- v18.8.0
- v18.7.0
- v18.6.0
- v18.5.0
- v18.4.0
- v18.3.0
- v18.2.0
- v18.1.0
- v18.0.0
- v17.9.1
- v17.9.0
- v17.8.0
- v17.7.2
- v17.7.1
- v17.7.0
- v17.6.0
- v17.5.0
- v17.4.0
- v17.3.1
- v17.3.0
- v17.2.0
- v17.1.0
- v17.0.1
- v17.0.0
- v16.20.2
- v16.20.1
- v16.20.0
- v16.19.1
- v16.19.0
- v16.18.1
- v16.18.0
- v16.17.1
- v16.17.0
- v16.16.0
- v16.15.1
- v16.15.0
- v16.14.2
- v16.14.1
- v16.14.0
- v16.13.2
- v16.13.1
- v16.13.0
- v16.12.0
- v16.11.1
- v16.11.0
- v16.10.0
- v16.9.1
- v16.9.0
- v16.8.0
- v16.7.0
- v16.6.2
- v16.6.1
- v16.6.0
- v16.5.0
- v16.4.2
- v16.4.1
- v16.4.0
- v16.3.0
- v16.2.0
- v16.1.0
- v16.0.0
- v15.14.0
- v15.13.0
- v15.12.0
- v15.11.0
- v15.10.0
- v15.9.0
- v15.8.0
- v15.7.0
- v15.6.0
- v15.5.1
- v15.5.0
- v15.4.0
- v15.3.0
- v15.2.1
- v15.2.0
- v15.1.0
- v15.0.1
- v15.0.0
Showing
3 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const Countdown = require('../common/countdown'); | ||
|
||
assert.throws(() => new http.Agent({ | ||
maxTotalSockets: 'test', | ||
}), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError', | ||
message: 'The "maxTotalSockets" argument must be of type number. ' + | ||
"Received type string ('test')", | ||
}); | ||
|
||
[-1, 0, NaN].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}`, | ||
}); | ||
}); | ||
|
||
assert.ok(new http.Agent({ | ||
maxTotalSockets: Infinity, | ||
})); | ||
|
||
function start(param = {}) { | ||
const { maxTotalSockets, maxSockets } = param; | ||
|
||
const agent = new http.Agent({ | ||
keepAlive: true, | ||
keepAliveMsecs: 1000, | ||
maxTotalSockets, | ||
maxSockets, | ||
maxFreeSockets: 3 | ||
}); | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.end('hello world'); | ||
}, 6)); | ||
const server2 = http.createServer(common.mustCall((req, res) => { | ||
res.end('hello world'); | ||
}, 6)); | ||
|
||
server.keepAliveTimeout = 0; | ||
server2.keepAliveTimeout = 0; | ||
|
||
const countdown = new Countdown(12, () => { | ||
assert.strictEqual(getRequestCount(), 0); | ||
agent.destroy(); | ||
server.close(); | ||
server2.close(); | ||
}); | ||
|
||
function handler(s) { | ||
for (let i = 0; i < 6; i++) { | ||
http.get({ | ||
host: 'localhost', | ||
port: s.address().port, | ||
agent, | ||
path: `/${i}`, | ||
}, common.mustCall((res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.resume(); | ||
res.on('end', common.mustCall(() => { | ||
for (const key of Object.keys(agent.sockets)) { | ||
assert(agent.sockets[key].length <= maxSockets); | ||
} | ||
assert(getTotalSocketsCount() <= maxTotalSockets); | ||
countdown.dec(); | ||
})); | ||
})); | ||
} | ||
} | ||
|
||
function getTotalSocketsCount() { | ||
let num = 0; | ||
for (const key of Object.keys(agent.sockets)) { | ||
num += agent.sockets[key].length; | ||
} | ||
return num; | ||
} | ||
|
||
function getRequestCount() { | ||
let num = 0; | ||
for (const key of Object.keys(agent.requests)) { | ||
num += agent.requests[key].length; | ||
} | ||
return num; | ||
} | ||
|
||
server.listen(0, common.mustCall(() => handler(server))); | ||
server2.listen(0, common.mustCall(() => handler(server2))); | ||
} | ||
|
||
// If maxTotalSockets is larger than maxSockets, | ||
// then the origin check will be skipped | ||
// when the socket is removed. | ||
[{ | ||
maxTotalSockets: 2, | ||
maxSockets: 3, | ||
}, { | ||
maxTotalSockets: 3, | ||
maxSockets: 2, | ||
}, { | ||
maxTotalSockets: 2, | ||
maxSockets: 2, | ||
}].forEach(start); |