From c1031915f7520c11597a564e4d4506f36086bdd4 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 15 Apr 2015 15:56:08 -0300 Subject: [PATCH 1/2] clustering! --- bws.js | 35 +++++++++++++++++++++++++++++------ config.js | 22 +++++++++++++--------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/bws.js b/bws.js index 51471e5..b3914d3 100755 --- a/bws.js +++ b/bws.js @@ -6,13 +6,36 @@ var config = require('./config'); var port = process.env.BWS_PORT || config.port || 3232; -var app = ExpressApp.start(config); -//app.listen(port); +var cluster = require('cluster'); +var http = require('http'); +var numCPUs = require('os').cpus().length; -var server = require('http').Server(app); -var ws = WsApp.start(server, config); +var startOne = function() { + var app = ExpressApp.start(config); + //app.listen(port); + var server = require('http').Server(app); + var ws = WsApp.start(server, config); + server.listen(port); + console.log('Bitcore Wallet Service running on port ' + port); +}; -server.listen(port); +if (!config.cluster) { + startOne(); +} else { + if (!config.storageOpts.multiLevel || !config.lockOpts.lockerServer) + throw 'When running in cluster mode, multilevel and locker server need to be configured'; -console.log('Bitcore Wallet Service running on port ' + port); + var clusterInstances = config.clusterInstances || numCpus; + + if (cluster.isMaster) { + for (var i = 0; i < clusterInstances; i++) { + cluster.fork(); + } + cluster.on('exit', function(worker, code, signal) { + console.log('worker ' + worker.process.pid + ' died'); + }); + } else { + startOne(); + } +} diff --git a/config.js b/config.js index f58e934..f0a242e 100644 --- a/config.js +++ b/config.js @@ -2,19 +2,23 @@ var config = { basePath: '/bws/api', disableLogs: false, port: 3232, + // Uncomment to make BWS a forking server + cluster: true, + // Uncomment to use the nr of availalbe CPUs + clusterInstances: 4, storageOpts: { - dbPath: './db', - // multiLevel: { - // host: 'localhost', - // port: 3230, - // }, + // dbPath: './db', + multiLevel: { + host: 'localhost', + port: 3230, + }, }, lockOpts: { // To use locker-server, uncomment this: - // lockerServer: { - // host: 'localhost', - // port: 3231, - // }, + lockerServer: { + host: 'localhost', + port: 3231, + }, }, blockchainExplorerOpts: { livenet: { From 7f912cdc700854349cdbf2674c7c788cf251bfd7 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 15 Apr 2015 16:25:31 -0300 Subject: [PATCH 2/2] add ssl support --- bws.js | 13 +++++++++++-- config.js | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bws.js b/bws.js index b3914d3..8102e3b 100755 --- a/bws.js +++ b/bws.js @@ -1,5 +1,7 @@ #!/usr/bin/env node +var fs = require('fs'); + var ExpressApp = require('./lib/expressapp'); var WsApp = require('./lib/wsapp'); var config = require('./config'); @@ -10,11 +12,18 @@ var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; +var serverModule = config.https ? require('https') : require('http'); +var serverOpts = {}; + +if (config.https) { + serverOpts.key = fs.readFileSync(config.privateKeyFile || './ssl/privatekey.pem'); + serverOpts.cert = fs.readFileSync(config.certificateFile || './ssl/certificate.pem'); +} var startOne = function() { var app = ExpressApp.start(config); - //app.listen(port); - var server = require('http').Server(app); + var server = config.https ? serverModule.createServer(serverOpts, app) : + serverModule.Server(app); var ws = WsApp.start(server, config); server.listen(port); console.log('Bitcore Wallet Service running on port ' + port); diff --git a/config.js b/config.js index f0a242e..6c0ba86 100644 --- a/config.js +++ b/config.js @@ -6,6 +6,11 @@ var config = { cluster: true, // Uncomment to use the nr of availalbe CPUs clusterInstances: 4, + + // https: true, + // privateKeyFile: 'private.pem', + // certificateFile: 'cert.pem', + storageOpts: { // dbPath: './db', multiLevel: {