Skip to content

Instantly share code, notes, and snippets.

@erkiesken
Created January 14, 2015 15:31
Show Gist options
  • Save erkiesken/bab01a8d8add5c033ca3 to your computer and use it in GitHub Desktop.
Save erkiesken/bab01a8d8add5c033ca3 to your computer and use it in GitHub Desktop.
Node cluster worker blocking send test
var cluster = require("cluster");
var blocked = require("blocked");
var debug = require("debug");
var workers;
if (cluster.isMaster) {
debug = debug("master-"+process.pid);
workers = {};
cluster.fork();
cluster.on("online", function(worker) {
workers[worker.process.pid] = worker;
debug("worker " + worker.process.pid + " came online");
});
cluster.on("exit", function(worker) {
delete workers[worker.process.pid];
debug("worker " + worker.process.pid + " died");
});
setInterval(function ()
{
var onlinePids = Object.keys(workers);
if (!onlinePids.length) {
return;
}
var worker = workers[onlinePids[0]];
debug("starting data send to %s", worker.process.pid);
worker.send(Array(1024*1024).join("#"));
debug("sent data to %s", worker.process.pid);
}, 500);
}
else if (cluster.isWorker)
{
debug = debug("worker-" + process.pid);
process.on('message', function(msg) {
debug("received %s bytes", msg.length);
debug("starting our work");
Array(100000000).join('a');
debug("finished our work");
});
}
blocked(function (ms) {
debug("!! BLOCKED for %sms", ms | 0);
});
debug("started process");
npm install debug blocked
DEBUG=* node app.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment