forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.go
83 lines (73 loc) · 1.5 KB
/
signals.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
package main
import (
"io/ioutil"
"os"
"os/signal"
"runtime/pprof"
"syscall"
)
type SignalHandler struct {
signalChannel chan os.Signal
stopChannel chan int
}
func (s *SignalHandler) run() {
run := true
for run {
select {
case sig := <-s.signalChannel:
s.handleSignal(sig)
case <-s.stopChannel:
signal.Stop(s.signalChannel)
run = false
}
}
}
func (s *SignalHandler) handleSignal(signal os.Signal) {
switch signal {
case syscall.SIGUSR1:
tf, err := ioutil.TempFile("", "block")
if err != nil {
log.Error(err.Error())
break
}
if err := pprof.Lookup("block").WriteTo(tf, 1); err != nil {
log.Error(err.Error())
tf.Close()
os.Remove(tf.Name())
break
}
if err := tf.Close(); err != nil {
log.Error(err.Error())
break
}
log.Info("Dumped block pprof to " + tf.Name())
case syscall.SIGUSR2:
tf, err := ioutil.TempFile("", "goroutine")
if err != nil {
log.Error(err.Error())
break
}
if err := pprof.Lookup("goroutine").WriteTo(tf, 2); err != nil {
log.Error(err.Error())
tf.Close()
os.Remove(tf.Name())
break
}
if err := tf.Close(); err != nil {
log.Error(err.Error())
break
}
log.Info("Dumped goroutine pprof to " + tf.Name())
}
}
func (s *SignalHandler) Stop() {
s.stopChannel <- 1
}
func MakeSignalHandler() *SignalHandler {
s := new(SignalHandler)
s.stopChannel = make(chan int)
s.signalChannel = make(chan os.Signal, 2)
signal.Notify(s.signalChannel, syscall.SIGUSR1, syscall.SIGUSR2)
go s.run()
return s
}