/
interrupt.go
70 lines (56 loc) · 1.63 KB
/
interrupt.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
package streamutil
import (
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
)
// Interrupt returns a channel that receives a signal when the application
// receives either an SIGINT or SIGTERM signal. This is provided for convenience
// when dealing with a select statement and receiving stream messages, making it
// easy to cleanly exit after fully handling one message, but before handling
// the next message.
func Interrupt() <-chan os.Signal {
ch := make(chan os.Signal, 3)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
return ch
}
// HandleInterrupts monitors for an interrupt signal, and calls the provided
// closer function once received. It has a built-in timeout capability to force
// terminate the application when the closer takes too long to close, or returns
// an error during closing.
func HandleInterrupts(signals chan os.Signal, closer func() error, logger *zap.Logger) {
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
s, ok := <-signals
if !ok {
return
}
logger.Info(
"Got interrupt signal, cleaning up. Use ^C to exit immediately.",
zap.String("signal", s.String()),
)
go func() {
abort := make(chan os.Signal, 1)
defer close(abort)
signal.Notify(abort, os.Interrupt)
<-abort
os.Exit(1)
}()
go func() {
time.Sleep(3 * time.Second)
logger.Fatal(
"Timed out while closing after receiving signal. Terminating.",
zap.String("signal", s.String()),
)
}()
err := closer()
if err != nil {
logger.Fatal(
"Error while closing after receiving signal. Terminating.",
zap.String("signal", s.String()),
zap.Error(err),
)
}
os.Exit(0)
}