Skip to content

Commit

Permalink
cluster: use Map to track indexes
Browse files Browse the repository at this point in the history
PR-URL: #23125
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
  • Loading branch information
cjihrig authored and targos committed Oct 3, 2018
1 parent 64f840a commit 2dd157f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/internal/cluster/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -141,7 +145,7 @@ function rr(message, indexesKey, cb) {

send({ act: 'close', key });
delete handles[key];
delete indexes[indexesKey];
indexes.delete(indexesKey);
key = undefined;
}

Expand Down

0 comments on commit 2dd157f

Please sign in to comment.