From 2dd157fbf34c5b864099a2aa53f1d40b5651227f Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 27 Sep 2018 10:08:48 -0400 Subject: [PATCH] cluster: use Map to track indexes 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 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/internal/cluster/child.js b/lib/internal/cluster/child.js index 39e891214ff787..05cb6d25e81a9d 100644 --- a/lib/internal/cluster/child.js +++ b/lib/internal/cluster/child.js @@ -8,7 +8,7 @@ const Worker = require('internal/cluster/worker'); const { internal, sendHelper } = require('internal/cluster/utils'); const cluster = new EventEmitter(); const handles = {}; -const indexes = {}; +const indexes = new Map(); const noop = () => {}; module.exports = cluster; @@ -62,14 +62,18 @@ cluster._getServer = function(obj, options, cb) { options.addressType, options.fd ].join(':'); - if (indexes[indexesKey] === undefined) - indexes[indexesKey] = 0; + let index = indexes.get(indexesKey); + + if (index === undefined) + index = 0; else - indexes[indexesKey]++; + index++; + + indexes.set(indexesKey, index); const message = util._extend({ act: 'queryServer', - index: indexes[indexesKey], + index, data: null }, options); @@ -108,7 +112,7 @@ function shared(message, handle, indexesKey, cb) { handle.close = function() { send({ act: 'close', key }); delete handles[key]; - delete indexes[indexesKey]; + indexes.delete(indexesKey); return close.apply(this, arguments); }.bind(handle); assert(handles[key] === undefined); @@ -141,7 +145,7 @@ function rr(message, indexesKey, cb) { send({ act: 'close', key }); delete handles[key]; - delete indexes[indexesKey]; + indexes.delete(indexesKey); key = undefined; }