-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
http: add maxTotalSockets to agent class #33617
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
Closed
Closed
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9255fa9
http: add maxTotalSockets to agent class
rickyes 958f2b3
fixup
rickyes 53ce024
fixup
rickyes 773d9f0
fixup
rickyes 5dffb12
fixup
rickyes ca96505
fixup
rickyes 154eb28
fixup
rickyes 62118af
fixup
rickyes eabfaee
fixup
rickyes d7148e3
fixup
rickyes 1eebc86
fixup
rickyes 25b6fa5
fixup
rickyes 2931fa9
fixup
rickyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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((param) => start(param)); | ||
rickyes marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.