node.js - Node js - Cluster delay before delegating request -
i'm using cluster module node.js benefit multiple cpu cores (4 in case). problem is, if worker busy, next request delegated available worker after 10 seconds, unacceptable. i'm using infinite loop test cluster follows:
var cluster = require ('cluster'); if (cluster.ismaster) { var cpucount = require('os').cpus().length; (var = 0; < cpucount-1; ++i) { cluster.fork(); } } else { var express = require('express'); var app = express(); app.get('/', function (req, res) { console.log('served worker %d!', cluster.worker.id); while(true) {} console.log("done"); res.status(200).send(); }); app.listen(8080, function () { console.log('worker %d running!', cluster.worker.id); }); } cluster.on('exit', function (worker) { console.log('worker %d died :(', worker.id); cluster.fork(); });
at first open localhost:8080 in browser, , log message "served worker 1" printed instantly. open same page in new tab, , after 10 seconds, "served worker 2" printed. why this, , how request delegated available worker?
which core runs load-balancer
method? maybe balancer busy because of infinite loop send request other app.
in fact for..loop
works synchronously , if load-balancer
method in same app , core loop
load balancer never works if there inifinite loop.
i recommend using of pm2 create cluster. library -that has been developed years- provides more expectations.
good luck..
Comments
Post a Comment