clustering in node.js is not working. Only one worker is always responding

后端 未结 2 1470
囚心锁ツ
囚心锁ツ 2021-01-13 11:47

I am practising clustering in node.js I am having a two core CPU. I created two workers and each worker runs a simple http server. Server response callback will block for 5

2条回答
  •  孤城傲影
    2021-01-13 12:16

    The while loop you have in your response code could be causing some serious problems. You should be using a setTimeout there if you want to simulate a long running request.

    Try this for your worker instead:

    http.createServer(function(req, res) {
        console.log('worker:' + cluster.worker.id + " going to send response ");
        setTimeout(function() {
           res.writeHead(200);
           res.end("hello world. worker: " + cluster.worker.id);
        }, 5000);
    }).listen(8000);
    

    That being said, what @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. Using setTimeout like I suggested will actually make your workers more available to handle requests so it's likely that you'll only see Worker 1 handle requests if you're just hitting the server manually from a browser. You might need some sort of load testing script to see both workers handling requests.

    As for the difference you're seeing between F5 and CTRL-F5, my best guess is that your browser is keeping the connection to the server alive so, when you just use F5, it uses the same connection which will always go to the same worker. When you use CTRL-F5 it's actually closing the connection that it had before and therefor could connect to either worker on the next request.

提交回复
热议问题