From 36139f8e94d46c460678d01e2d7f6372df2df1ec Mon Sep 17 00:00:00 2001 From: John J Date: Sat, 5 Mar 2016 17:49:59 -0500 Subject: [PATCH 1/3] Fixed so each goroutine does not make an instance of the redis client, instead we can pool it across the goroutines --- server.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/server.go b/server.go index db15e4b..71903ae 100644 --- a/server.go +++ b/server.go @@ -3,23 +3,29 @@ package main import ( "fmt" "net/http" + "runtime" + "gopkg.in/redis.v3" ) -func handler(w http.ResponseWriter, r *http.Request) { - //fmt.Fprint(w, "Hello World!"); - client := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", - DB: 0, - }) +type Server struct { + db *redis.Client +} - results, _ := client.HGetAll("keystone:dynatron/metric::ebis-wlri-analysis:info").Result() +func (s *Server) handler(w http.ResponseWriter, r *http.Request) { + results, _ := s.db.HGetAll("keystone:dynatron/metric::ebis-wlri-analysis:info").Result() fmt.Fprint(w, results) - //fmt.Printf("The type is: %s \n", results) } func main() { - http.HandleFunc("/", handler) - http.ListenAndServe(":3000", nil); + runtime.GOMAXPROCS(1) + client := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", + DB: 0, + }) + server := &Server{db: client} + + http.HandleFunc("/", server.handler) + http.ListenAndServe(":3000", nil) } From f20e78a80d0afb5030df54cdf0b4d07b1b9ccc3d Mon Sep 17 00:00:00 2001 From: John Johnson Date: Sat, 5 Mar 2016 17:57:47 -0500 Subject: [PATCH 2/3] Took out the runtime processor limiting --- server.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/server.go b/server.go index 71903ae..9aeccf3 100644 --- a/server.go +++ b/server.go @@ -3,7 +3,6 @@ package main import ( "fmt" "net/http" - "runtime" "gopkg.in/redis.v3" ) @@ -18,7 +17,6 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) { } func main() { - runtime.GOMAXPROCS(1) client := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", From 0ce3cd4026011058d5fd1eff6056aa58be760ec1 Mon Sep 17 00:00:00 2001 From: John J Date: Sat, 5 Mar 2016 18:17:43 -0500 Subject: [PATCH 3/3] Adding clustering to nodejs --- server-cluster.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 server-cluster.js diff --git a/server-cluster.js b/server-cluster.js new file mode 100644 index 0000000..b7a66d8 --- /dev/null +++ b/server-cluster.js @@ -0,0 +1,36 @@ +var cluster = require('cluster'); +var http = require('http'); +var numCPUs = require('os').cpus().length; +// var redis = require('redis'); +// var client = redis.createClient(); + +if (cluster.isMaster) { + for (var i = 0; i < numCPUs; i++) { + cluster.fork(); + } +// } else { +// http.createServer(function(request, response) { +// client.hgetall('perftest', function(err, results) { +// response.writeHead(200, { 'Content-Type': 'application/json'}); +// response.write(JSON.stringify(results)); +// response.end(); +// }); +// }).listen(3001, function() { +// console.log('listener spun up on localhost:3001') +// }); +// } + +// other way, creating a client per forked thread +} else { + var redis = require('redis'); + var client = redis.createClient(); + http.createServer(function(request, response) { + client.hgetall('keystone:dynatron/metric::ebis-wlri-analysis:info', function(err, results) { + response.writeHead(200, { 'Content-Type': 'application/json'}); + response.write(JSON.stringify(results)); + response.end(); + }); + }).listen(3000, function() { + console.log('listener spun up on localhost:3000') + }); +}