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: refactor agent.destroy() #28596

Closed
Closed
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
20 changes: 10 additions & 10 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,18 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
};

Agent.prototype.destroy = function destroy() {
const sets = [this.freeSockets, this.sockets];
for (var s = 0; s < sets.length; s++) {
var set = sets[s];
var keys = Object.keys(set);
for (var v = 0; v < keys.length; v++) {
var setName = set[keys[v]];
for (var n = 0; n < setName.length; n++) {
setName[n].destroy();
}
destroySockets(this.freeSockets);
destroySockets(this.sockets);
};

function destroySockets(sockets) {
for (const setKey in sockets) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

briefer:

Suggested change
for (const setKey in sockets) {
for (const socket of Object.values(sockets).flat()) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But probably also much less performant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in changes the behavior though. Before, the prototype would not be checked but now it does.

I do not know why flat was suggested. It should already be a flat array?

I think in this specific case it should be fine to use Object.values, since it should not be a hot path to destroy the sockets.

var setName = sockets[setKey];
trivikr marked this conversation as resolved.
Show resolved Hide resolved
for (const socket of setName) {
socket.destroy();
}
}
};
}

function handleSocketCreation(agent, request, informRequest) {
return function handleSocketCreation_Inner(err, socket) {
Expand Down