diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index 457b3579b12c1c..2eead0b8a3a70a 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -148,7 +148,7 @@ 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'); } } @@ -156,12 +156,10 @@ function removeWorker(worker) { 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) { @@ -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; @@ -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) @@ -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); }); @@ -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) { diff --git a/lib/internal/cluster/utils.js b/lib/internal/cluster/utils.js index 94b82d039a098a..c3e14cbb53a721 100644 --- a/lib/internal/cluster/utils.js +++ b/lib/internal/cluster/utils.js @@ -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();