-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapphelp.go
75 lines (64 loc) · 1.18 KB
/
apphelp.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
package main
import (
"flag"
"log"
"os"
"os/signal"
"runtime/pprof"
"syscall"
_ "net/http/pprof"
"github.com/google/gops/agent"
)
const (
ltracep = "trace: "
ldebugp = "debug: "
linfop = "info: "
lwarningp = "warning: "
lerrorp = "error: "
lalertp = "alert: "
)
var cpuprofile string
func init() {
flag.StringVar(&cpuprofile, "pprof", cpuprofile, "enable CPU pprof, supply file name.")
}
func SetupProfile() {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
}
func StopProfile() {
if cpuprofile != "" {
log.Println("save profile", cpuprofile)
pprof.StopCPUProfile()
}
}
// should block
func SetupSignal(exitFunc func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-c:
switch s {
case syscall.SIGINT:
info.Println("exiting...", s)
// os.Exit(0) // will not run defer
if exitFunc != nil {
exitFunc()
}
return // will run defer
default:
log.Println("unprocessed signal:", s)
}
}
}
}
func SetupGops() {
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
}