forked from zeromicro/go-zero
/
signals.go
56 lines (46 loc) · 954 Bytes
/
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
//go:build linux || darwin
package proc
import (
"os"
"os/signal"
"syscall"
"github.com/wuhc1010/go-zero/core/logx"
)
const timeFormat = "0102150405"
var done = make(chan struct{})
func init() {
go func() {
var profiler Stopper
// https://golang.org/pkg/os/signal/#Notify
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM)
for {
v := <-signals
switch v {
case syscall.SIGUSR1:
dumpGoroutines()
case syscall.SIGUSR2:
if profiler == nil {
profiler = StartProfile()
} else {
profiler.Stop()
profiler = nil
}
case syscall.SIGTERM:
select {
case <-done:
// already closed
default:
close(done)
}
gracefulStop(signals)
default:
logx.Error("Got unregistered signal:", v)
}
}
}()
}
// Done returns the channel that notifies the process quitting.
func Done() <-chan struct{} {
return done
}