cluster: use Map to track handles in cluster child

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>
This commit is contained in:
cjihrig 2018-09-27 10:28:52 -04:00
parent aa899aa6d0
commit 5a50989f5b
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -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();
}