Browse Source

Merge pull request #24 from miekg/master

Use expvar for global counter
Ask Bjørn Hansen 12 years ago
parent
commit
efa6742687
3 changed files with 19 additions and 13 deletions
  1. 2 1
      geodns.go
  2. 15 8
      monitor.go
  3. 2 4
      serve.go

+ 2 - 1
geodns.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"expvar"
 	"flag"
 	"log"
 	"net"
@@ -16,7 +17,7 @@ var gitVersion string
 var serverId string
 
 var timeStarted = time.Now()
-var qCounter uint64 = 0
+var qCounter = expvar.NewInt("qCounter")
 
 var (
 	flagconfig      = flag.String("config", "./dns/", "directory of zone files")

+ 15 - 8
monitor.go

@@ -3,6 +3,7 @@ package main
 import (
 	"code.google.com/p/go.net/websocket"
 	"encoding/json"
+	"expvar"
 	"io"
 	"log"
 	"net/http"
@@ -117,11 +118,12 @@ func initialStatus() string {
 
 func logStatus() {
 	log.Println(initialStatus())
-	lastQueryCount := qCounter
+	// Does not impact performance to much
+	lastQueryCount := expVarToInt64(qCounter)
 
 	for {
-		newQueries := qCounter - lastQueryCount
-		lastQueryCount = qCounter
+		newQueries := expVarToInt64(qCounter) - lastQueryCount
+		lastQueryCount = expVarToInt64(qCounter)
 		log.Println("goroutines", runtime.NumGoroutine(), "queries", newQueries)
 
 		time.Sleep(60 * time.Second)
@@ -137,15 +139,15 @@ func monitor() {
 	go hub.run()
 	go httpHandler()
 
-	lastQueryCount := qCounter
+	lastQueryCount := expVarToInt64(qCounter)
 	for {
-		newQueries := qCounter - lastQueryCount
-		lastQueryCount = qCounter
+		newQueries := expVarToInt64(qCounter) - lastQueryCount
+		lastQueryCount = expVarToInt64(qCounter)
 
 		status := map[string]string{}
 		status["up"] = strconv.Itoa(int(time.Since(timeStarted).Seconds()))
-		status["qs"] = strconv.FormatUint(qCounter, 10)
-		status["qps"] = strconv.FormatUint(newQueries, 10)
+		status["qs"] = qCounter.String()
+		status["qps"] = strconv.FormatInt(newQueries, 10)
 
 		message, err := json.Marshal(status)
 
@@ -173,3 +175,8 @@ func httpHandler() {
 
 	log.Fatal(http.ListenAndServe(*flaghttp, nil))
 }
+
+func expVarToInt64(i *expvar.Int) (j int64) {
+	j, _ = strconv.ParseInt(i.String(), 10, 64)
+	return 
+}

+ 2 - 4
serve.go

@@ -27,9 +27,7 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 	logPrintf("[zone %s] incoming %s %s %d from %s\n", z.Origin, req.Question[0].Name,
 		dns.TypeToString[qtype], req.MsgHdr.Id, w.RemoteAddr())
 
-	// is this safe/atomic or does it need to go through a channel?
-	qCounter++
-
+	qCounter.Add(1)
 	logPrintln("Got request", req)
 
 	label := getQuestionName(z, req)
@@ -173,7 +171,7 @@ func statusRR(z *Zone) []dns.RR {
 		status["h"] = hostname
 	}
 	status["up"] = strconv.Itoa(int(time.Since(timeStarted).Seconds()))
-	status["qs"] = strconv.FormatUint(qCounter, 10)
+	status["qs"] = qCounter.String()
 
 	js, err := json.Marshal(status)