Skip to content

Commit

Permalink
fixed express through throng and using the cluster library directly
Browse files Browse the repository at this point in the history
  • Loading branch information
fcirone committed Jul 21, 2016
1 parent f799027 commit 025f897
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
72 changes: 37 additions & 35 deletions index-throng.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,56 @@
const throng = require('throng');

var WORKERS = process.env.WEB_CONCURRENCY || 1;
var PORT = process.env.PORT || 8001;
var PORT = process.env.PORT || 3131;

// throng({
// workers: WORKERS,
// lifetime: Infinity,
// start: startApp
// });

throng(start);

function start() {
console.log('started');
}
throng(startApp);

function startApp() {
var express = require('express');
var crypto = require('crypto');
var app = express();

app.listen(PORT, onListen);
// app
// .get('/cpu', cpuBound)
// .get('/memory', memoryBound)
// .get('/io', ioBound)
// .listen(PORT, onListen);

// function cpuBound(req, res, next) {
// var key = Math.random() < 0.5 ? 'ninjaturtles' : 'powerrangers';
// var hmac = crypto.createHmac('sha512WithRSAEncryption', key);
// var date = Date.now() + '';
// hmac.setEncoding('base64');
// hmac.end(date, function() {
// res.send('A hashed date for you! ' + hmac.read());
// });
// }
//
// function memoryBound(req, res, next) {
// var hundredk = new Array(100 * 1024).join('X');
// setTimeout(function sendResponse() {
// res.send('Large response: ' + hundredk);
// }, 20).unref();
// }
//
// function ioBound(req, res, next) {
// setTimeout(function SimulateDb() {
// res.send('Got response from fake db!');
// }, 300).unref();
// }
app.get('/', function (request, response) {
console.log('Request to worker %d', cluster.worker.id);
response.send('Worker ' + cluster.worker.id);
});

app.listen(PORT, onListen);
app.get('/cpu', cpuBound);
app.get('/memory', memoryBound)
app.get('/io', ioBound)

function cpuBound(req, res, next) {
var key = Math.random() < 0.5 ? 'ninjaturtles' : 'powerrangers';
var hmac = crypto.createHmac('sha512WithRSAEncryption', key);
var date = Date.now() + '';
hmac.setEncoding('base64');
hmac.end(date, function() {
res.send('A hashed date for you! ' + hmac.read());
});
}

function memoryBound(req, res, next) {
var hundredk = new Array(100 * 1024).join('X');

console.log('PID %d', process.pid);

setTimeout(function sendResponse() {
res.send('Large response: ' + hundredk);
}, 2000).unref();
}

function ioBound(req, res, next) {
setTimeout(function SimulateDb() {
res.send('Got response from fake db!');
}, 300).unref();
}

function onListen() {
console.log('Listening on', PORT);
Expand Down
46 changes: 21 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
var express = require('express');
var cluster = require('cluster');
var os = require('os');//no need to download anything

var PORT = process.env.PORT || 8001;
if (cluster.isMaster) {

if(cluster.isMaster) {
var numWorkers = os.cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
var cpuCount = require('os').cpus().length;

for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function (worker) {
console.log('Worker %d died :(', worker.id);
cluster.fork();
});

cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
} else {
var app = require('express')();

app.listen(PORT, function() {
console.log('Express server listening on %d, in %s mode', PORT, app.get('env'));
});
var express = require('express');
var app = express();

app.get('/', function (request, response) {
console.log('Request to worker %d', cluster.worker.id);
response.send('Worker ' + cluster.worker.id);
});

// // Start server
// server.listen(config.port, config.ip, function () {
// console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
// });
app.listen(3131);
console.log('Worker %d running!', cluster.worker.id);
}

0 comments on commit 025f897

Please sign in to comment.