Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Created April 6, 2013 08:46
Show Gist options
  • Select an option

  • Save divanvisagie/5325441 to your computer and use it in GitHub Desktop.

Select an option

Save divanvisagie/5325441 to your computer and use it in GitHub Desktop.
A constantly updated gist of a nice cluster pattern
var http = require( 'http' ),
cluster = require( 'cluster' ),
numCPUs = require( 'os' ).cpus().length;
if ( cluster.isMaster ){
for ( var i = 0 ; i < numCPUs; i++ ){
cluster.fork();
}
cluster.on( 'exit' , function( worker, code, signal ){
console.log( 'worker' , worker.process.pid , 'died' , 'code:', code , 'signal:' , signal );
} );
cluster.on('online', function(worker) {
console.log( 'worker' , worker.id , 'is up' );
});
} else {
http.createServer( function( req, res ){
console.log( 'there was a request' , timeouts );
res.writeHead( 200 );
res.end( 'Hello World' );
} ).listen( process.argv[2] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment