Skip to content

Commit

Permalink
cluster: use Map to track handles in master
Browse files Browse the repository at this point in the history
PR-URL: #23125
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
  • Loading branch information
cjihrig committed Sep 30, 2018
1 parent 5a50989 commit 847037e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,18 @@ function removeWorker(worker) {
delete cluster.workers[worker.id];

if (keys(cluster.workers).length === 0) {
assert(keys(handles).length === 0, 'Resource leak detected.');
assert(handles.size === 0, 'Resource leak detected.');
intercom.emit('disconnect');
}
}

function removeHandlesForWorker(worker) {
assert(worker);

for (var key in handles) {
const handle = handles[key];

handles.forEach((handle, key) => {
if (handle.remove(worker))
delete handles[key];
}
handles.delete(key);
});
}

cluster.fork = function(env) {
Expand Down Expand Up @@ -277,7 +275,7 @@ function queryServer(worker, message) {

const key = `${message.address}:${message.port}:${message.addressType}:` +
`${message.fd}:${message.index}`;
var handle = handles[key];
var handle = handles.get(key);

if (handle === undefined) {
let address = message.address;
Expand All @@ -302,12 +300,13 @@ function queryServer(worker, message) {
constructor = SharedHandle;
}

handles[key] = handle = new constructor(key,
address,
message.port,
message.addressType,
message.fd,
message.flags);
handle = new constructor(key,
address,
message.port,
message.addressType,
message.fd,
message.flags);
handles.set(key, handle);
}

if (!handle.data)
Expand All @@ -319,11 +318,11 @@ function queryServer(worker, message) {
errno: errno,
key: key,
ack: message.seq,
data: handles[key].data
data: handles.get(key).data
}, reply);

if (errno)
delete handles[key]; // Gives other workers a chance to retry.
handles.delete(key); // Gives other workers a chance to retry.

send(worker, reply, handle);
});
Expand All @@ -346,10 +345,10 @@ function listening(worker, message) {
// removed by a prior call to removeHandlesForWorker() so guard against that.
function close(worker, message) {
const key = message.key;
const handle = handles[key];
const handle = handles.get(key);

if (handle && handle.remove(worker))
delete handles[key];
handles.delete(key);
}

function send(worker, message, handle, cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/cluster/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const util = require('util');
module.exports = {
sendHelper,
internal,
handles: {} // Used in tests.
handles: new Map() // Used in tests.
};

const callbacks = new Map();
Expand Down

0 comments on commit 847037e

Please sign in to comment.