-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
executable file
·60 lines (49 loc) · 896 Bytes
/
signal.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 config
import (
"os"
"os/signal"
"sync"
"go.uber.org/zap"
)
type stop struct {
channel chan struct{}
sync.WaitGroup
}
var (
// Stop is the global stop instance
Stop = &stop{
channel: make(chan struct{}),
}
// Handle signals
signalChannel = make(chan os.Signal, 1)
)
func init() {
// Stop flag indicates if ctrl+c has been sent
signal.Notify(signalChannel, os.Interrupt)
// Handle signals
go func() {
for {
for sig := range signalChannel {
switch sig {
case os.Interrupt:
zap.S().Info("Received interrupt...")
close(Stop.channel)
return
}
}
}
}()
}
// Chan returns a read only channel that is closed when the program exits
func (s *stop) Chan() <-chan struct{} {
return s.channel
}
// Bool returns t/f if we should stop
func (s *stop) Bool() bool {
select {
case <-s.channel:
return true
default:
return false
}
}