-
Notifications
You must be signed in to change notification settings - Fork 8
/
signal.go
47 lines (37 loc) · 1.02 KB
/
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
package utils
import (
"os"
"os/signal"
"syscall"
)
// RegisterExitHandlers registers a shutdown function to be called when a signal is received.
func RegisterExitHandlers(shutdownFuncs ...func()) (stop chan struct{}) {
stop = make(chan struct{})
go func() {
// Block until any signal is received.
<-stop
// execute our shutdown functions
for _, f := range shutdownFuncs {
f()
}
}()
return stop
}
// RegisterOSExitHandlers registers a shutdown function to be called when a signal is received.
func RegisterOSExitHandlers(shutdownFuncs ...func()) (stop chan struct{}) {
var exitSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL}
stop = make(chan struct{})
s := make(chan os.Signal, len(exitSignals))
signal.Notify(s, exitSignals...)
go func() {
// Wait for a signal from the OS before dispatching
// a stop signal to all other goroutines observing this channel.
<-s
close(stop)
// execute our shutdown functions
for _, f := range shutdownFuncs {
f()
}
}()
return stop
}