Skip to content

Commit 94cc285

Browse files
committed
Create cluster_app.js to enable simple process forking; readme addition
1 parent 9267d94 commit 94cc285

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ node app.js
8282
> application and automatically restart the server. Once installed, instead of `node app.js` use `nodemon app.js`.
8383
> It is a big time saver in the long run.
8484
85+
> **Cluster**: You can execute an instance of `app.js` for each CPU by calling `node cluster_app.js` instead of `node app.js`
86+
8587
Next up, if you want to use any of the APIs or OAuth authentication methods, you will need to obtain
8688
appropriate credentials: Client ID, Client Secret, API Key, or Username & Password. You will
8789
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
212214
### I don't need a sticky footer, can I delete it?
213215
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.
214216

217+
### What is cluster_app.js?
218+
Per the [documentation](http://nodejs.org/api/cluster.html):
219+
> A single instance of Node runs in a single thread. To take advantage of multi-core systems
220+
> the user will sometimes want to launch a cluster of Node processes to handle the load.
221+
> The cluster module allows you to easily create child processes that all share server ports.
222+
223+
`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.
224+
215225
TODO
216226
----
217227
- 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).

cluster_app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
5+
var os = require('os');
6+
var cluster = require('cluster');
7+
8+
/**
9+
* Cluster setup.
10+
*/
11+
12+
// Setup the cluster to use app.js
13+
cluster.setupMaster({
14+
exec: 'app.js'
15+
});
16+
17+
// Listen for dying workers
18+
cluster.on('exit', function(worker) {
19+
console.log('Worker ' + worker.id + ' died');
20+
// Replace the dead worker
21+
cluster.fork();
22+
});
23+
24+
// Fork a worker for each available CPU
25+
for (var i = 0; i < os.cpus().length; i++) {
26+
cluster.fork();
27+
}

0 commit comments

Comments
 (0)