Reputation: 635
I am using a Windows 7 machine to test clustering in NodeJS.
The code is below:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
cluster.on('listening', function(worker, address) {
console.log("A worker with #" + worker.id + " is now connected to " + address.address + ":" + address.port);
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function (req, res) {
console.log(cluster.worker.id + ' is responding..!!!');
var j = 0;
//long running task
for (var i = 0; i < 10000000000; i++) {
j++;
}
res.writeHead(200);
res.end("hello world\n");
}).listen(8080);
}
When I run this code, it looks like all the request are sent to last worker that was created. All the remaining workers are just sitting idle while all the requests are being processed by the node that was created last in the sequential order.
I have tried to do some reach but it seems that no one has reported this yet. I saw a post on Stack Overflow with same issue and there is no solution. I am using Node v0.10.26.
Please point me in the right direction.
Upvotes: 4
Views: 3191
Reputation: 21
actually it is the feature of chrome and some other browsers if both the request are same chrome waits for first request to finish and then save the result and pass it on so to avoid this do ctrl+shift+r this refreshes page without cache
Upvotes: 0
Reputation: 1
require('os').cpus().length is number of logical processors . if you go to taskManager -> Performance -> CPU , you will see number of logicalProcessor
Upvotes: 0
Reputation: 11
I've just used two browsers + curl from command line. I've got the same result: http://prntscr.com/8fxx4g
But correct answer is here clustering in node.js is not working. Only one worker is always responding:
@dandavis said in the comments is true: cluster does not do round-robin load balancing so, as long as Worker 1 is available for requests, it will handle them.
Upvotes: 1
Reputation: 635
This answer is for others if they have the same issue. Use different browsers to make HTTP requests which will make sure that different workers are being used.
Upvotes: 6