2

I am using the core cluster module in node v0.6.5. I have the following code:

var cluster = require('cluster');
var http = require('http');
var numWorkers = 3;

var count = 0;

if (cluster.isMaster) {
  for (var i = 0; i < numWorkers; i++) {
    cluster.fork();
  }
} else {
  console.log('createServer called');
  http.createServer(function (req, res) {
    count++;
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Count is: ' + count.toString());
  }).listen(1337, "127.0.0.1");
}

Everytime I hit the page, the count is incremented twice, e.g. 1,3,5,7... Why does it increment twice?

1 Answer 1

5

This is probably another case where the browser is silently requesting /favicon.ico, thus making there 2 requests per page.

Sign up to request clarification or add additional context in comments.

2 Comments

Gotcha. Another small doubt: the child workers share the global variables is it? Meaning - the child processes utilize the CPU cores right? But, they still have access to the global variable?
That I'm not familiar with enough to help you on, I just knew about the browser's silent request there. Sorry :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.