Skip to content

Commit

Permalink
cluster: use Map to track callbacks
Browse files Browse the repository at this point in the history
Use a Map to avoid delete operations in callback tracking.

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 3101096 commit 22f51a6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/internal/cluster/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
handles: {} // Used in tests.
};

const callbacks = {};
const callbacks = new Map();
var seq = 0;

function sendHelper(proc, message, handle, cb) {
Expand All @@ -18,7 +18,7 @@ function sendHelper(proc, message, handle, cb) {
message = util._extend({ cmd: 'NODE_CLUSTER' }, message);

if (typeof cb === 'function')
callbacks[seq] = cb;
callbacks.set(seq, cb);

message.seq = seq;
seq += 1;
Expand All @@ -34,9 +34,13 @@ function internal(worker, cb) {

var fn = cb;

if (message.ack !== undefined && callbacks[message.ack] !== undefined) {
fn = callbacks[message.ack];
delete callbacks[message.ack];
if (message.ack !== undefined) {
const callback = callbacks.get(message.ack);

if (callback !== undefined) {
fn = callback;
callbacks.delete(message.ack);
}
}

fn.apply(worker, arguments);
Expand Down

0 comments on commit 22f51a6

Please sign in to comment.