Skip to content

Commit

Permalink
cluster: use Map to track handles in cluster child
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
cjihrig authored and targos committed Oct 3, 2018
1 parent 2dd157f commit 0f133eb
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/internal/cluster/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {};

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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 });
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 0f133eb

Please sign in to comment.