-
Notifications
You must be signed in to change notification settings - Fork 53
/
handler.go
92 lines (75 loc) · 2.55 KB
/
handler.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package zpprof
import (
"fmt"
"net/http"
"net/http/pprof"
"time"
"github.com/sohaha/zlsgo/znet"
)
var startTime = time.Now()
func infoHandler(c *znet.Context) {
m := NewSystemInfo(startTime)
info := fmt.Sprintf("%s:%s\n", "服务器", m.ServerName)
info += fmt.Sprintf("%s:%s\n", "运行时间", m.Runtime)
info += fmt.Sprintf("%s:%s\n", "goroutine数量", m.GoroutineNum)
info += fmt.Sprintf("%s:%s\n", "CPU核数", m.CPUNum)
info += fmt.Sprintf("%s:%s\n", "当前内存使用量", m.UsedMem)
info += fmt.Sprintf("%s:%s\n", "当前堆内存使用量", m.HeapInuse)
info += fmt.Sprintf("%s:%s\n", "总分配的内存", m.TotalMem)
info += fmt.Sprintf("%s:%s\n", "系统内存占用量", m.SysMem)
info += fmt.Sprintf("%s:%s\n", "指针查找次数", m.Lookups)
info += fmt.Sprintf("%s:%s\n", "内存分配次数", m.Mallocs)
info += fmt.Sprintf("%s:%s\n", "内存释放次数", m.Frees)
info += fmt.Sprintf("%s:%s\n", "距离上次GC时间", m.LastGCTime)
info += fmt.Sprintf("%s:%s\n", "下次GC内存回收量", m.NextGC)
info += fmt.Sprintf("%s:%s\n", "GC暂停时间总量", m.PauseTotalNs)
info += fmt.Sprintf("%s:%s\n", "上次GC暂停时间", m.PauseNs)
_, _ = fmt.Fprint(c.Writer, info)
}
func indexHandler(c *znet.Context) {
pprof.Index(c.Writer, c.Request)
}
func allocsHandler(c *znet.Context) {
pprof.Handler("allocs").ServeHTTP(c.Writer, c.Request)
}
func mutexHandler(c *znet.Context) {
pprof.Handler("mutex").ServeHTTP(c.Writer, c.Request)
}
func heapHandler(c *znet.Context) {
pprof.Handler("heap").ServeHTTP(c.Writer, c.Request)
}
func goroutineHandler(c *znet.Context) {
pprof.Handler("goroutine").ServeHTTP(c.Writer, c.Request)
}
func blockHandler(c *znet.Context) {
pprof.Handler("block").ServeHTTP(c.Writer, c.Request)
}
func threadCreateHandler(c *znet.Context) {
pprof.Handler("threadcreate").ServeHTTP(c.Writer, c.Request)
}
func cmdlineHandler(c *znet.Context) {
pprof.Cmdline(c.Writer, c.Request)
}
func profileHandler(c *znet.Context) {
pprof.Profile(c.Writer, c.Request)
}
func symbolHandler(c *znet.Context) {
pprof.Symbol(c.Writer, c.Request)
}
func traceHandler(c *znet.Context) {
pprof.Trace(c.Writer, c.Request)
}
func redirectPprof(c *znet.Context) {
http.Redirect(c.Writer, c.Request, "/debug/pprof/", http.StatusFound)
}
func authDebug(token string) znet.HandlerFunc {
return func(c *znet.Context) {
getToken := c.DefaultQuery("token", c.GetCookie("debug-token"))
c.SetCookie("debug-token", token, 600)
if token == "" || getToken == token {
c.Next()
} else {
c.Byte(401, []byte("No permission"))
}
}
}