From 0f133eb5a36e9e89a9ed5081312db53feada0af4 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 27 Sep 2018 10:28:52 -0400 Subject: [PATCH] cluster: use Map to track handles in cluster child MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/23125 Reviewed-By: Gus Caplan Reviewed-By: Michaƫl Zasso Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat Reviewed-By: Denys Otrishko --- lib/internal/cluster/child.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/internal/cluster/child.js b/lib/internal/cluster/child.js index 05cb6d25e81a9d..13a22b0186fc28 100644 --- a/lib/internal/cluster/child.js +++ b/lib/internal/cluster/child.js @@ -7,7 +7,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols; const Worker = require('internal/cluster/worker'); const { internal, sendHelper } = require('internal/cluster/utils'); const cluster = new EventEmitter(); -const handles = {}; +const handles = new Map(); const indexes = new Map(); const noop = () => {}; @@ -111,12 +111,12 @@ function shared(message, handle, indexesKey, cb) { handle.close = function() { send({ act: 'close', key }); - delete handles[key]; + handles.delete(key); indexes.delete(indexesKey); return close.apply(this, arguments); }.bind(handle); - assert(handles[key] === undefined); - handles[key] = handle; + assert(handles.has(key) === false); + handles.set(key, handle); cb(message.errno, handle); } @@ -144,7 +144,7 @@ function rr(message, indexesKey, cb) { return; send({ act: 'close', key }); - delete handles[key]; + handles.delete(key); indexes.delete(indexesKey); key = undefined; } @@ -166,15 +166,15 @@ function rr(message, indexesKey, cb) { handle.getsockname = getsockname; // TCP handles only. } - assert(handles[key] === undefined); - handles[key] = handle; + assert(handles.has(key) === false); + handles.set(key, handle); cb(0, handle); } // Round-robin connection. function onconnection(message, handle) { const key = message.key; - const server = handles[key]; + const server = handles.get(key); const accepted = server !== undefined; send({ ack: message.seq, accepted }); @@ -207,17 +207,16 @@ function _disconnect(masterInitiated) { } } - for (var key in handles) { - const handle = handles[key]; - delete handles[key]; + handles.forEach((handle) => { waitingCount++; if (handle[owner_symbol]) handle[owner_symbol].close(checkWaitingCount); else handle.close(checkWaitingCount); - } + }); + handles.clear(); checkWaitingCount(); }