forked from xiaomeng79/istio-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpprof.go
62 lines (56 loc) · 1.01 KB
/
pprof.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
package pprof
import (
"log"
"net/http"
_ "net/http/pprof" // pprof
"os"
"os/signal"
"strconv"
"syscall"
)
const (
Addr = ":38888"
PidFile = "server.pid"
)
func Run() {
ch := make(chan os.Signal, 1)
ch1 := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1) // 开启pprof
signal.Notify(ch1, syscall.SIGUSR2) // 关闭pprof
// 将pid写入文件
f, err := os.Create(PidFile)
if err != nil {
log.Printf("%v", err)
}
_, err = f.WriteString(strconv.Itoa(os.Getpid()))
if err != nil {
log.Printf("%v", err)
}
f.Close()
log.Print("进程id: ", os.Getpid())
var server *http.Server
for {
select {
case <-ch:
go func() {
server = &http.Server{
Addr: Addr,
}
log.Print("listen addr: ", Addr)
err := server.ListenAndServe()
if err != nil {
log.Printf("listen:%v", err)
}
}()
case <-ch1:
if server != nil {
err := server.Close()
if err != nil {
log.Printf("server close:%v", err)
} else {
server = nil
}
}
}
}
}