-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
128 lines (113 loc) · 3.33 KB
/
handler.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package signal
import (
"os"
"os/signal"
"sync"
)
type Handler struct {
hooks map[os.Signal][]func(os.Signal)
ignored []os.Signal
mutex *sync.Mutex
channel chan os.Signal
listening bool
}
// TODO: Should it be optional to declare the channel handling here so the
// design where one uses signal handling to hold open the application?
func NewHandler() Handler {
handler := Handler{
hooks: map[os.Signal][]func(os.Signal){},
ignored: []os.Signal{},
mutex: &sync.Mutex{},
channel: make(chan os.Signal, 1),
}
go func() {
for {
incomingSignal := <-handler.channel
handler.handle(incomingSignal)
}
}()
return handler
}
func ShutdownHandler(function func(os.Signal)) Handler {
handler := NewHandler()
return handler.OnShutdown(function)
}
func (self Handler) handle(incomingSignal os.Signal) {
functions := self.hooks[incomingSignal]
for _, function := range functions {
function(incomingSignal)
}
}
// /////////////////////////////////////////////////////////////////////////////
func (h Handler) Add(function func(os.Signal), signals ...os.Signal) Handler {
h.mutex.Lock()
defer h.mutex.Unlock()
for _, s := range signals {
h.hooks[s] = append(h.hooks[s], function)
signal.Notify(h.channel, s)
}
return h
}
// NOTE: Shutdown adds the defined hook to all shutdown/exit signals
func (h Handler) OnShutdown(function func(os.Signal)) Handler {
return h.Add(function, ShutdownSignals...)
}
func (h Handler) OnInterrupt(function func(os.Signal)) Handler {
return h.Add(function, Interrupt)
}
func (h Handler) OnTerminate(function func(os.Signal)) Handler {
return h.Add(function, Terminate)
}
func (h Handler) OnQuit(function func(os.Signal)) Handler { return h.Add(function, Quit) }
func (h Handler) OnHangup(function func(os.Signal)) Handler { return h.Add(function, Hangup) }
func (h Handler) OnKill(function func(os.Signal)) Handler { return h.Add(function, Kill) }
// /////////////////////////////////////////////////////////////////////////////
// TODO: Would be nice to eventually build in functionality to turly ignore
// signals even force kill signals; possibly via uninterruptable sleep or some
// similar technique to make this a truly general use signal handling library
func (h Handler) Ignore(signals ...os.Signal) Handler {
h.ignored = append(h.ignored, signals...)
for _, ignoredSignal := range h.ignored {
h.Remove(ignoredSignal)
signal.Ignore(h.Signals()...)
}
return h
}
// NOTE: Stop ignoring will stop listening too since it requires use of reset
// which resets both Notify and Ignore calls
func (h Handler) StopIgnoring() Handler {
signal.Reset()
return h
}
func (h Handler) StopListening() Handler {
signal.Stop(h.channel)
return h
}
// /////////////////////////////////////////////////////////////////////////////
func (h Handler) Remove(s os.Signal) Handler {
h.mutex.Lock()
defer h.mutex.Unlock()
delete(h.hooks, s)
return h
}
func (h Handler) Clear() Handler {
h.mutex.Lock()
defer h.mutex.Unlock()
for s := range h.hooks {
delete(h.hooks, s)
}
return h
}
func (h Handler) Reset() Handler {
h.Clear()
h.StopListening()
return h
}
// /////////////////////////////////////////////////////////////////////////////
func (h Handler) Signals() []os.Signal {
signals := make([]os.Signal, len(h.hooks))
for s, _ := range h.hooks {
signals = append(signals, s)
}
return signals
}