Skip to content

Instantly share code, notes, and snippets.

@Francois-Esquire
Created August 19, 2018 00:37
Show Gist options
  • Save Francois-Esquire/cb385615361b8e56fa40beb81280fd91 to your computer and use it in GitHub Desktop.
Save Francois-Esquire/cb385615361b8e56fa40beb81280fd91 to your computer and use it in GitHub Desktop.
Node.js Cluster usage with simple worker delegation
const cluster = require('cluster');
const MULTIPLY_EVENT = 'multiply';
if (cluster.isMaster) {
(function run({ dataset, compare, length }) {
if (global.gc) global.gc();
const results = [];
const workers = [];
for (let i = 0; i < require('os').cpus().length; i += 1) {
const worker = cluster.fork();
workers.push(worker);
worker.on('message', data => {
results.push(data.result);
workers.push(worker);
});
}
// instead of a while loop, which shuts down the event loop.
setInterval(() => {
if (length === results.length) {
// all our work is done.
const total = results.reduce((value, next) => value + next, 0);
console.log(`total: ${total}, comparison: ${compare}`);
process.exit();
} else {
const worker = workers.shift();
// check if worker is available
if (worker) {
const set = dataset.pop();
// data remains
if (set) worker.send({ type: MULTIPLY_EVENT, set });
}
}
}, 0);
})(
(function generateDataset(iterations) {
let dataset;
if (iterations && typeof iterations === 'number') {
const magnitude = 100;
const gen = () => Math.trunc(Math.random() * magnitude);
dataset = Array(iterations)
.fill('', -1 * iterations)
.map(() => [gen(), gen()]);
} else dataset = [[99, 2], [11, 2], [3, 3], [8, 9], [11, 4]];
const compare = dataset.reduce((value, next) => {
const [a, b] = next;
return value + a * b;
}, 1);
return { dataset, compare, length: dataset.length };
})(12 /* number of iterations */),
);
} else {
process.on('message', data => {
switch (data.type) {
default:
break;
case MULTIPLY_EVENT:
process.send({
result: data.set.reduce((value, next) => value * next, 1),
});
break;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment