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 Worker = require('internal/cluster/worker');
const { internal, sendHelper } = require('internal/cluster/utils'); const { internal, sendHelper } = require('internal/cluster/utils');
const cluster = new EventEmitter(); const cluster = new EventEmitter();
const handles = {}; const handles = new Map();
const indexes = new Map(); const indexes = new Map();
const noop = () => {}; const noop = () => {};
@ -111,12 +111,12 @@ function shared(message, handle, indexesKey, cb) {
handle.close = function() { handle.close = function() {
send({ act: 'close', key }); send({ act: 'close', key });
delete handles[key]; handles.delete(key);
indexes.delete(indexesKey); indexes.delete(indexesKey);
return close.apply(this, arguments); return close.apply(this, arguments);
}.bind(handle); }.bind(handle);
assert(handles[key] === undefined); assert(handles.has(key) === false);
handles[key] = handle; handles.set(key, handle);
cb(message.errno, handle); cb(message.errno, handle);
} }
@ -144,7 +144,7 @@ function rr(message, indexesKey, cb) {
return; return;
send({ act: 'close', key }); send({ act: 'close', key });
delete handles[key]; handles.delete(key);
indexes.delete(indexesKey); indexes.delete(indexesKey);
key = undefined; key = undefined;
} }
@ -166,15 +166,15 @@ function rr(message, indexesKey, cb) {
handle.getsockname = getsockname; // TCP handles only. handle.getsockname = getsockname; // TCP handles only.
} }
assert(handles[key] === undefined); assert(handles.has(key) === false);
handles[key] = handle; handles.set(key, handle);
cb(0, handle); cb(0, handle);
} }
// Round-robin connection. // Round-robin connection.
function onconnection(message, handle) { function onconnection(message, handle) {
const key = message.key; const key = message.key;
const server = handles[key]; const server = handles.get(key);
const accepted = server !== undefined; const accepted = server !== undefined;
send({ ack: message.seq, accepted }); send({ ack: message.seq, accepted });
@ -207,17 +207,16 @@ function _disconnect(masterInitiated) {
} }
} }
for (var key in handles) { handles.forEach((handle) => {
const handle = handles[key];
delete handles[key];
waitingCount++; waitingCount++;
if (handle[owner_symbol]) if (handle[owner_symbol])
handle[owner_symbol].close(checkWaitingCount); handle[owner_symbol].close(checkWaitingCount);
else else
handle.close(checkWaitingCount); handle.close(checkWaitingCount);
} });
handles.clear();
checkWaitingCount(); checkWaitingCount();
} }