Skip to content

Commit

Permalink
new example
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Aug 18, 2014
1 parent 10df786 commit 95f9693
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
15 changes: 15 additions & 0 deletions session-cluster-redis/controllers/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.install = function(framework) {
framework.route('/', view_homepage, ['#session']);
};

function view_homepage() {
var self = this;

if (self.session.counter === undefined)
self.session.counter = 0;
else
self.session.counter++;

process.send('Response framework ID: ' + framework.id + ' (' + self.session.counter + ')');
self.view('homepage');
}
30 changes: 30 additions & 0 deletions session-cluster-redis/definitions/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var redis = require('redis');

// Install session module
INSTALL('module', 'https://modules.totaljs.com/session/v1.00/session.js');

// Configure session module with REDIS
framework.on('install', function(type, name) {

if (type !== 'module')
return;

if (name !== 'session')
return;

var session = MODULE('session').instance;

session.onRead = function(id, callback) {
var client = redis.createClient();
client.get('session_' + id, function(err, reply) {
client.quit();
callback(err ? {} : reply === null ? {} : JSON.parse(reply.toString()));
});
};

session.onWrite = function(id, value) {
var client = redis.createClient();
client.set('session_' + id, JSON.stringify(value));
client.quit();
};
});
43 changes: 43 additions & 0 deletions session-cluster-redis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var http = require('http');
var cluster = require('cluster');
var os = require('os');

var debug = true;

if (!cluster.isMaster) {

// This code will be executed according the number of CPU
// This code will be using: single process RAM * numCPUs
var framework = require('total.js');

// Set framework ID
framework.on('message', function(message) {
if (message.type === 'id')
framework.id = message.id;
});

framework.http('debug');
return;
}

var numCPUs = os.cpus().length;

for (var i = 0; i < numCPUs; i++) {

// Run framework
var fork = cluster.fork();

fork.on('message', onMessage);

// Send ID
fork.send({ type: 'id', id: i });
}

console.log('Cluster is running.');

function onMessage(message) {
console.log('Message ->', message);
}

// Use a terminal for testing:
// $ siege -b -r 10 http://127.0.0.1:8000/
3 changes: 3 additions & 0 deletions session-cluster-redis/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```
$ npm install redis
```
16 changes: 16 additions & 0 deletions session-cluster-redis/views/homepage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@{layout('')}

<!DOCTYPE html>
<html>
<head>
<title>node session + redis + cluster</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta name="format-detection" content="telephone=no"/>
<meta name="viewport" content="width=1024, user-scalable=yes" />
<meta name="robots" content="all,follow" />
</head>
<body>
Counter: @{session.counter}
</body>
</html>

0 comments on commit 95f9693

Please sign in to comment.