Skip to content

Commit

Permalink
[server] use cluster module
Browse files Browse the repository at this point in the history
  • Loading branch information
nleush committed Dec 18, 2013
1 parent 822241c commit 0adeb08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {

// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('fork', function(worker) {
console.log('--- Cluster: worker ' + worker.process.pid + ' started');
});
cluster.on('exit', function(worker, code, signal) {
console.log('--- Cluster: Worker ' + worker.process.pid + ' died (code: ' + code + '), restarting...');
cluster.fork();
});

} else {

require('./server');

if (CONFIG.CLUSTER_WORKER_RESTART_ON_MEMORY_USED) {
setInterval(function() {

var mem = process.memoryUsage().rss;
if (mem > CONFIG.CLUSTER_WORKER_RESTART_ON_MEMORY_USED) {
console.log('--- Cluster: worker ' + process.pid + ' used too much memory, exiting...');
process.exit(1);
}

}, 1000);
}
}
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

CACHE_TTL_PAGE_TIMEOUT: 10 * 60,

CLUSTER_WORKER_RESTART_ON_MEMORY_USED: 300 * 1024 * 1024, // 300 MB.

metaLoadingTimeout: 5 * 1000,
USER_AGENT: "Mozilla/5.0 (compatible; Iframely/" + version + "; +http://iframely.com/)",

Expand Down

0 comments on commit 0adeb08

Please sign in to comment.