/
notify.go
60 lines (53 loc) · 1.11 KB
/
notify.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
package server
import (
"math"
"os"
"os/signal"
"syscall"
"time"
)
type notify struct {
server *server
systemSignal chan os.Signal
lifeCycleLimit chan struct{}
lifeCycleTimer *time.Timer
lifeCycleTime chan time.Duration
}
func (n *notify) init(srv *server) *notify {
n.server = srv
n.systemSignal = make(chan os.Signal, 1)
n.lifeCycleLimit = make(chan struct{}, 1)
n.lifeCycleTime = make(chan time.Duration, 1)
n.lifeCycleTimer = time.NewTimer(math.MaxInt64)
n.lifeCycleTimer.Stop()
signal.Notify(n.systemSignal, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
return n
}
func (n *notify) close() {
close(n.systemSignal)
close(n.lifeCycleLimit)
close(n.lifeCycleTime)
}
func (n *notify) run() {
defer func() {
if err := n.server.Shutdown(); err != nil {
panic(err)
}
n.close()
}()
for {
select {
case <-n.systemSignal:
return
case <-n.lifeCycleLimit:
n.systemSignal <- syscall.SIGQUIT
case <-n.lifeCycleTimer.C:
n.systemSignal <- syscall.SIGQUIT
case d := <-n.lifeCycleTime:
n.lifeCycleTimer.Stop()
if d > 0 {
n.lifeCycleTimer.Reset(d)
}
}
}
}