Skip to content

Commit 8289da3

Browse files
committed
Start the WorkerPool in a better way
1 parent 02ba3ef commit 8289da3

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/server/balancer/route.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
var Cookies = Npm.require('cookies');
22

3-
var workers = process.env['CLUSTER_WORKERS_COUNT'] || 0;
4-
workers = new WorkerPool(workers);
3+
var workers = null;
4+
5+
// We need to start the worker pool after the server binded
6+
// This allow use to play nicely with tools like userdown
7+
WebApp.onListening(function() {
8+
var workersCount = Balancer._getWorkersCount();
9+
workers = new WorkerPool(workersCount);
10+
});
511

612
Balancer.handleHttp = function handleHttp(req, res) {
713
if(!Cluster.discovery) return processHereHTTP();
@@ -109,7 +115,7 @@ function processHereHTTP() {
109115
function processHereWS(req, socket, head) {
110116
if(process.env['CLUSTER_WORKER_ID']) return;
111117

112-
var worker = workers.pickWorker();
118+
var worker = workers && workers.pickWorker();
113119
// No worker, can't proxy. So process here.
114120
if(!worker) return false;
115121

lib/server/balancer/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
var urlParse = Npm.require('url').parse;
22
var urlResolve = Npm.require('url').resolve;
3+
var os = Npm.require('os');
4+
5+
Balancer._getWorkersCount = function() {
6+
var workersCount = process.env['CLUSTER_WORKERS_COUNT'];
7+
if(("" + workersCount).toLowerCase() === "auto") {
8+
workersCount = os.cpus().length;
9+
}
10+
11+
// We don't need to start a worker in this case
12+
if(workersCount == 1) {
13+
workersCount = 0;
14+
}
15+
16+
return workersCount;
17+
};
318

419
Balancer._buildCookie = function _buildCookie(name, service) {
520
return name + "::" + service;

0 commit comments

Comments
 (0)