Permalink
Browse files

use the cluster module for multicore goodness

  • Loading branch information...
1 parent d259616 commit e4302f694d0175a46bfbb16a79f6da69c00bb06b @earldouglas earldouglas committed Nov 27, 2012
Showing with 24 additions and 11 deletions.
  1. +23 −10 node.js/app.js
  2. +1 −1 readme/fast-test.dat
  3. BIN readme/fast-test.png
View
@@ -29,14 +29,27 @@ app.get('/medium', scamper.medium);
//sleep 300ms
app.get('/slow', scamper.slow);
-/**
- * Run Node.js Server
- */
-var server = http.createServer(app).listen(9000, '127.0.0.1', function(){
- /**
- * Start Message
- */
- console.log("Node.js Web server listening on port 9000");
-});
-
+// cluster module
+// http://nodejs.org/api/cluster.html#cluster_cluster
+
+var cluster = require('cluster');
+var http = require('http');
+var numCPUs = require('os').cpus().length;
+
+if (cluster.isMaster) {
+ // Fork workers.
+ for (var i = 0; i < numCPUs; i++) {
+ cluster.fork();
+ }
+
+ cluster.on('exit', function(worker, code, signal) {
+ console.log('worker ' + worker.process.pid + ' died');
+ });
+} else {
+ // Workers can share any TCP connection
+ // In this case its a HTTP server
+ http.createServer(app).listen(9000, '127.0.0.1', function(){
+ console.log("Node.js Web server listening on port 9000");
+ });
+}
View
@@ -4,7 +4,7 @@
"Finagle" 0
"Finatra" 10803
"Lift" 4742
-"Node.js" 7929
+"Node.js" 29005
"Pinky" 4249
"Play 2" 16184
"Play 2 mini" 16715
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e4302f6

Please sign in to comment.