Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2013 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7325816 to your computer and use it in GitHub Desktop.
Save anonymous/7325816 to your computer and use it in GitHub Desktop.
Problem with cluster in Node.js (Express)
var cluster = require('cluster');
function fibonacci(n) {
if (n < 2) {
return 1;
} else {
return fibonacci(n - 2) + fibonacci(n - 1);
}
}
if (cluster.isMaster) {
for (var i = 0; i < require('os').cpus().length; i += 1) {
cluster.fork();
}
} else {
var app = require('express')();
app.get('/slow', function(req, res) {
fibonacci(43);
res.set('x-pid', cluster.worker.id + ' pid=' + process.pid);
res.send('SLOW: worker ' + cluster.worker.id + ' pid=' + process.pid + '<img src="a"><img src="b"><img src="c"><img src="d"><img src="e">');
});
app.get('/fast', function(req, res) {
res.set('x-pid', cluster.worker.id + ' pid=' + process.pid);
res.send('FAST: worker ' + cluster.worker.id + ' pid=' + process.pid + '<img src="a"><img src="b"><img src="c"><img src="d"><img src="e">');
});
app.get(/^\/\w$/, function(req, res) {
res.set('x-pid', cluster.worker.id + ' pid=' + process.pid);
res.send('');
});
app.listen(8080);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment