diff options
author | cjihrig <cjihrig@gmail.com> | 2018-09-27 18:13:49 +0300 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2018-09-30 18:04:10 +0300 |
commit | 847037eaeddbb4ad493a6686f61f8b2d03266ff2 (patch) | |
tree | 06f19df9f5eb8d51d3ce6a3df18b8441a938ee5d /lib/internal/cluster | |
parent | 5a50989f5bde1e7cd495922444a3ce5a4c680897 (diff) |
cluster: use Map to track handles in master
PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'lib/internal/cluster')
-rw-r--r-- | lib/internal/cluster/master.js | 33 | ||||
-rw-r--r-- | lib/internal/cluster/utils.js | 2 |
2 files changed, 17 insertions, 18 deletions
diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index 457b3579b12..2eead0b8a3a 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 94b82d039a0..c3e14cbb53a 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(); |