From 64e5ece974df01dd9f60af000b13940c19a05747 Mon Sep 17 00:00:00 2001 From: danielamitay Date: Tue, 28 Jan 2014 03:04:04 -0500 Subject: [PATCH] Create cluster_app.js to enable simple process forking; readme addition --- README.md | 10 ++++++++++ cluster_app.js | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 cluster_app.js diff --git a/README.md b/README.md index 01dc53bc5b..cafc99041a 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ node app.js > application and automatically restart the server. Once installed, instead of `node app.js` use `nodemon app.js`. > It is a big time saver in the long run. +> **Cluster**: You can execute an instance of `app.js` for each CPU by calling `node cluster_app.js` instead of `node app.js` + Next up, if you want to use any of the APIs or OAuth authentication methods, you will need to obtain appropriate credentials: Client ID, Client Secret, API Key, or Username & Password. You will need to go through each provider to generate new credentials. @@ -212,6 +214,14 @@ For the sake of simplicity. While there might be a better approach, such as pass ### I don't need a sticky footer, can I delete it? Absolutely. But unlike a regular footer there is a bit more work involved. First, delete `#wrap` and `#footer` *ID*s from **styles.less**. Next delete `#wrap` and `#footer` from **layout.jade**. If no element is specified before the class or id, Jade assumes it's a `div` element. Don't forget to indent everything under `#wrap` to the left once, since this project uses two spaces per block indentation. +### What is cluster_app.js? +Per the [documentation](http://nodejs.org/api/cluster.html): +> A single instance of Node runs in a single thread. To take advantage of multi-core systems +> the user will sometimes want to launch a cluster of Node processes to handle the load. +> The cluster module allows you to easily create child processes that all share server ports. + +`cluster_app.js` allows you to take advantage of this feature by forking a process of `app.js` for each CPU detected. For the majority of applications serving HTTP requests, this is a resounding boon. However, the cluster module is still considered **"Stability: 1 - Experimental"**, therefore it should only be used after understanding it's purpose and behavior. + TODO ---- - Concatenate and minify all assets via Express middleware if possible, otherwise Gulp.js. Because even with caching enabled, there is at least 50-80ms delay for each static file request (On Heroku). diff --git a/cluster_app.js b/cluster_app.js new file mode 100755 index 0000000000..cc05200b90 --- /dev/null +++ b/cluster_app.js @@ -0,0 +1,27 @@ +/** + * Module dependencies. + */ + +var os = require('os'); +var cluster = require('cluster'); + +/** + * Cluster setup. + */ + +// Setup the cluster to use app.js +cluster.setupMaster({ + exec: 'app.js' +}); + +// Listen for dying workers +cluster.on('exit', function(worker) { + console.log('Worker ' + worker.id + ' died'); + // Replace the dead worker + cluster.fork(); +}); + +// Fork a worker for each available CPU +for (var i = 0; i < os.cpus().length; i++) { + cluster.fork(); +}