Created
July 6, 2012 21:14
-
-
Save nf/3062783 to your computer and use it in GitHub Desktop.
Hello world web server that scales across multiple processors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.pid + ' died'); | |
}); | |
} else { | |
// Workers can share any TCP connection | |
// In this case its a HTTP server | |
http.createServer(function(req, res) { | |
res.writeHead(200); | |
res.end("hello world\n"); | |
}).listen(8000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"runtime" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
handler := func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("hello world\n")) | |
} | |
http.ListenAndServe(":8000", http.HandlerFunc(handler)) | |
} |
wow
wow 🫡
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍