node/lib/internal/cluster/utils.js
cjihrig 47a0a0b503
cluster: move handle tracking out of utils
internal/cluster/utils.js exported a handles object, which was
used in a test. That test, test-cluster-disconnect-handles.js,
was removed in https://github.com/nodejs/node/pull/12495. This
commit moves the handles object to the only file in the codebase
that still uses it.

PR-URL: https://github.com/nodejs/node/pull/23131
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-10-01 08:58:24 -04:00

47 lines
1 KiB
JavaScript

'use strict';
const util = require('util');
module.exports = {
sendHelper,
internal
};
const callbacks = new Map();
var seq = 0;
function sendHelper(proc, message, handle, cb) {
if (!proc.connected)
return false;
// Mark message as internal. See INTERNAL_PREFIX in lib/child_process.js
message = util._extend({ cmd: 'NODE_CLUSTER' }, message);
if (typeof cb === 'function')
callbacks.set(seq, cb);
message.seq = seq;
seq += 1;
return proc.send(message, handle);
}
// Returns an internalMessage listener that hands off normal messages
// to the callback but intercepts and redirects ACK messages.
function internal(worker, cb) {
return function onInternalMessage(message, handle) {
if (message.cmd !== 'NODE_CLUSTER')
return;
var fn = cb;
if (message.ack !== undefined) {
const callback = callbacks.get(message.ack);
if (callback !== undefined) {
fn = callback;
callbacks.delete(message.ack);
}
}
fn.apply(worker, arguments);
};
}