-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
109 lines (93 loc) · 2.69 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Heavily inspired by https://github.com/mraksoll4/btcd/blob/master/signal.go
// Copyright (C) 2015-2017 The Lightning Network Developers
package signal
import (
"os"
"os/signal"
"syscall"
)
var (
// interruptChannel is used to receive SIGINT (Ctrl+C) signals.
interruptChannel = make(chan os.Signal, 1)
// shutdownRequestChannel is used to request the daemon to shutdown
// gracefully, similar to when receiving SIGINT.
shutdownRequestChannel = make(chan struct{})
// quit is closed when instructing the main interrupt handler to exit.
quit = make(chan struct{})
// shutdownChannel is closed once the main interrupt handler exits.
shutdownChannel = make(chan struct{})
)
func init() {
signalsToCatch := []os.Signal{
os.Interrupt,
os.Kill,
syscall.SIGABRT,
syscall.SIGTERM,
syscall.SIGQUIT,
}
signal.Notify(interruptChannel, signalsToCatch...)
go mainInterruptHandler()
}
// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
// interruptChannel and shutdown requests on the shutdownRequestChannel, and
// invokes the registered interruptCallbacks accordingly. It also listens for
// callback registration.
// It must be run as a goroutine.
func mainInterruptHandler() {
// isShutdown is a flag which is used to indicate whether or not
// the shutdown signal has already been received and hence any future
// attempts to add a new interrupt handler should invoke them
// immediately.
var isShutdown bool
// shutdown invokes the registered interrupt handlers, then signals the
// shutdownChannel.
shutdown := func() {
// Ignore more than one shutdown signal.
if isShutdown {
log.Infof("Already shutting down...")
return
}
isShutdown = true
log.Infof("Shutting down...")
// Signal the main interrupt handler to exit, and stop accept
// post-facto requests.
close(quit)
}
for {
select {
case signal := <-interruptChannel:
log.Infof("Received %v", signal)
shutdown()
case <-shutdownRequestChannel:
log.Infof("Received shutdown request.")
shutdown()
case <-quit:
log.Infof("Gracefully shutting down.")
close(shutdownChannel)
return
}
}
}
// Alive returns true if the main interrupt handler has not been killed.
func Alive() bool {
select {
case <-quit:
return false
default:
return true
}
}
// RequestShutdown initiates a graceful shutdown from the application.
func RequestShutdown() {
select {
case shutdownRequestChannel <- struct{}{}:
case <-quit:
}
}
// ShutdownChannel returns the channel that will be closed once the main
// interrupt handler has exited.
func ShutdownChannel() <-chan struct{} {
return shutdownChannel
}