forked from totaljs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10df786
commit 95f9693
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
``` | ||
$ npm install redis | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |