Merge branch 'cluster' into develop

This commit is contained in:
James Willcox 2014-06-18 12:08:08 -05:00
Родитель 297e583f3c 2f81eab2c7
Коммит a9f2941e85
4 изменённых файлов: 71 добавлений и 22 удалений

Просмотреть файл

@ -4,6 +4,19 @@ logging:
# (via Winston, https://github.com/flatiron/winston)
level: debug
# Node Cluster options
cluster:
enabled: false
workers:
# When true (the default), the number of workers
# is determined automatically based on number
# of CPUs on the machine.
auto: true
# When 'auto' is false, this number is used to
# determine the number of workers.
count: 8
# Gonzales server settings.
proxy:
# Gonzales listen port.

Просмотреть файл

@ -1,2 +1,5 @@
logging:
level: error
cluster:
enabled: true

Просмотреть файл

@ -1,8 +1,11 @@
'use strict';
var log = require('./log');
var cluster = require('cluster');
var pkg = require('../package.json');
var SpdyProxy = require('./proxy');
var PacServer = require('./pac');
var CONFIG = require('config');
var GONZALES_NAME = 'Gonzales';
@ -18,19 +21,21 @@ function showVersion() {
console.log(GONZALES_TITLE);
}
// Process command-line arguments and return options path.
(function processArgs() {
process.argv.forEach(function(value) {
if (value === '-h' || value === '--help') {
showHelp();
process.exit(1);
}
if (value === '-v' || value === '--version') {
showVersion();
process.exit(1);
}
});
})();
if (cluster.isMaster) {
// Process command-line arguments and return options path.
(function processArgs() {
process.argv.forEach(function(value) {
if (value === '-h' || value === '--help') {
showHelp();
process.exit(1);
}
if (value === '-v' || value === '--version') {
showVersion();
process.exit(1);
}
});
})();
}
function addVersionConfig() {
CONFIG.version = pkg.version;
@ -44,10 +49,34 @@ CONFIG.getConfigSources().forEach(function(config) {
log.debug('Using configuration %s', config.name);
});
var SpdyProxy = require('./proxy');
var proxy = new SpdyProxy(CONFIG);
proxy.listen(CONFIG.proxy.port);
function getWorkerCount() {
if (!CONFIG.cluster.enabled) {
return 0;
}
var PacServer = require('./pac');
var pacServer = new PacServer(CONFIG);
pacServer.listen(CONFIG.pac.port);
if (CONFIG.cluster.workers.auto) {
return require('os').cpus().length;
} else {
return CONFIG.cluster.workers.count;
}
}
if (CONFIG.cluster.enabled && cluster.isMaster) {
var count = getWorkerCount();
log.debug('starting %d workers', count);
for (var i = 0; i < count; ++i) {
cluster.fork();
}
cluster.on('exit', function(worker, code) {
log.debug('worker %d died, exit code %d',
worker.process.pid, code);
cluster.fork();
});
} else {
var pacServer = new PacServer(CONFIG);
pacServer.listen(CONFIG.pac.port);
var proxy = new SpdyProxy(CONFIG);
proxy.listen(CONFIG.proxy.port);
}

Просмотреть файл

@ -1,6 +1,10 @@
'use strict';
var winston = require('winston');
var cluster = require('cluster');
var label = cluster.isMaster ? 'Master ' + process.pid :
'Worker ' + cluster.worker.process.pid;
var level = require('config').logging.level;
@ -16,7 +20,7 @@ function createLogger(level, label) {
});
}
var logger = createLogger(level);
var logger = createLogger(level, label);
exports.log = logger.log.bind(logger);
exports.debug = logger.debug.bind(logger);
@ -24,7 +28,7 @@ exports.info = logger.info.bind(logger);
exports.warn = logger.warn.bind(logger);
exports.error = logger.error.bind(logger);
exports.logify = function(obj, label) {
var objectLogger = createLogger(level, label);
exports.logify = function(obj, objectLabel) {
var objectLogger = createLogger(level, label + ', ' + objectLabel);
objectLogger.extend(obj);
};