From 48ac70c0b80582c627c4cc3f90836f840aea2a09 Mon Sep 17 00:00:00 2001 From: James Willcox Date: Mon, 16 Jun 2014 23:19:14 -0500 Subject: [PATCH 1/2] Add clustering, enable by default in production mode --- config/default.yml | 13 +++++++++ config/production.yml | 3 ++ lib/index.js | 67 +++++++++++++++++++++++++++++++------------ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/config/default.yml b/config/default.yml index 162564d..f632e56 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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. diff --git a/config/production.yml b/config/production.yml index ab14738..7dca98b 100644 --- a/config/production.yml +++ b/config/production.yml @@ -1,2 +1,5 @@ logging: level: error + +cluster: + enabled: true diff --git a/lib/index.js b/lib/index.js index 1ea061c..739b677 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); +} From 2f81eab2c70a03d49577e14ea40f6b35e0fb9388 Mon Sep 17 00:00:00 2001 From: James Willcox Date: Tue, 17 Jun 2014 11:02:54 -0500 Subject: [PATCH 2/2] Add cluster-enabled logging --- lib/log.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/log.js b/lib/log.js index f2ec18f..d7e6e35 100644 --- a/lib/log.js +++ b/lib/log.js @@ -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); };