Created
January 14, 2015 15:31
-
-
Save erkiesken/bab01a8d8add5c033ca3 to your computer and use it in GitHub Desktop.
Node cluster worker blocking send test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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