-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_stats.go
77 lines (61 loc) · 1.55 KB
/
request_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
type statsAll struct {
TotalBytes int64
Nodes []*cacheStats
}
func (sta *statsAll) String() string {
b, _ := json.Marshal(sta)
return string(b)
}
func (sta *statsAll) write(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", sta)
}
func (c *cacheStats) String() string {
b, _ := json.Marshal(c)
return string(b)
}
func (c *cacheStats) write(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", c)
}
func cacheStatsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
statsop := &statsOp{
done: make(chan bool),
}
statsType := ps.ByName("stats_type")
log.Println("request::stats type", statsType)
timeitStart := time.Now().UnixNano()
mainCache.CacheStats <- statsop
<-statsop.done
if statsType == "local" {
statsop.stats.ElapsedNs = time.Now().UnixNano() - timeitStart
statsop.stats.write(w)
} else if statsType == "all" {
sta := &statsAll{}
sta.Nodes = append(sta.Nodes, statsop.stats)
sta.TotalBytes = statsop.stats.MemBytes
ch := siblingsMgr.propagateStats()
for stat := range ch {
if stat != nil {
sta.Nodes = append(sta.Nodes, stat)
sta.TotalBytes += stat.MemBytes
}
}
statsop.stats.ElapsedNs = time.Now().UnixNano() - timeitStart
sta.write(w)
} else {
e := newException("ResourceNotFoundException", "Resource not found")
e.write(w)
}
}