-
Notifications
You must be signed in to change notification settings - Fork 7
/
shutdown.go
163 lines (142 loc) · 3.79 KB
/
shutdown.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package shutdown provides convenient interface for working with os.Signal.
//
// Multiple hooks can be applied,
// they will be called simultaneously on app shutdown.
package shutdown
import (
"math/rand"
"os"
"os/signal"
"sync"
)
// DefaultShutdown is a default instance.
var DefaultShutdown = New()
// Shutdown is an instance of shutdown handler.
type Shutdown struct {
hooks map[string]func(os.Signal)
mutex *sync.Mutex
}
// New creates a new Shutdown instance.
func New() *Shutdown {
return &Shutdown{
hooks: map[string]func(os.Signal){},
mutex: &sync.Mutex{},
}
}
// Add adds a shutdown hook
// and returns hook identificator (key).
func Add(fn func()) string {
return DefaultShutdown.Add(fn)
}
// AddWithKey adds a shutdown hook
// with provided identificator (key).
func AddWithKey(key string, fn func()) {
DefaultShutdown.AddWithKey(key, fn)
}
// AddWithParam adds a shutdown hook with signal parameter
// and returns hook identificator (key).
func AddWithParam(fn func(os.Signal)) string {
return DefaultShutdown.AddWithParam(fn)
}
// AddWithKeyWithParam adds a shutdown hook with signal parameter
// with provided identificator (key).
func AddWithKeyWithParam(key string, fn func(os.Signal)) {
DefaultShutdown.AddWithKeyWithParam(key, fn)
}
// Hooks returns a copy of current hooks.
func Hooks() map[string]func(os.Signal) {
return DefaultShutdown.Hooks()
}
// Listen waits for provided OS signals.
// It will wait for any signal if no signals provided.
func Listen(signals ...os.Signal) {
DefaultShutdown.Listen(signals...)
}
// Remove cancels hook by identificator (key).
func Remove(key string) {
DefaultShutdown.Remove(key)
}
// Reset cancels all hooks.
func Reset() {
DefaultShutdown.Reset()
}
// Add adds a shutdown hook
// and returns hook identificator (key).
func (s *Shutdown) Add(fn func()) string {
return s.AddWithParam(func(os.Signal) {
fn()
})
}
// AddWithKey adds a shutdown hook
// with provided identificator (key).
func (s *Shutdown) AddWithKey(key string, fn func()) {
s.AddWithKeyWithParam(key, func(os.Signal) {
fn()
})
}
// AddWithParam adds a shutdown hook with signal parameter
// and returns hook identificator (key).
func (s *Shutdown) AddWithParam(fn func(os.Signal)) string {
key := randomKey()
s.AddWithKeyWithParam(key, fn)
return key
}
// AddWithKeyWithParam adds a shutdown hook with signal parameter
// with provided identificator (key).
func (s *Shutdown) AddWithKeyWithParam(key string, fn func(os.Signal)) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.hooks[key] = fn
}
// Hooks returns a copy of current hooks.
func (s *Shutdown) Hooks() map[string]func(os.Signal) {
s.mutex.Lock()
defer s.mutex.Unlock()
fns := map[string]func(os.Signal){}
for key, cb := range s.hooks {
fns[key] = cb
}
return fns
}
// Listen waits for provided OS signals.
// It will wait for any signal if no signals provided.
func (s *Shutdown) Listen(signals ...os.Signal) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, signals...)
sig := <-ch
var wg sync.WaitGroup
for _, fn := range s.Hooks() {
wg.Add(1)
go func(sig os.Signal, fn func(os.Signal)) {
defer wg.Done()
fn(sig)
}(sig, fn)
}
wg.Wait()
}
// Remove cancels hook by identificator (key).
func (s *Shutdown) Remove(key string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.hooks, key)
}
// Reset cancels all hooks.
func (s *Shutdown) Reset() {
s.mutex.Lock()
defer s.mutex.Unlock()
for key := range s.hooks {
delete(s.hooks, key)
}
}
// randomKey generates a random identificator (key) for hook.
//
// Do not use this identificator for purposes other then to remove a hook
// as long as it's not fairly random without seed.
func randomKey() string {
runes := []rune("0123456789abcdefghijklmnopqrstuvwxyz")
b := make([]rune, 16)
for i := range b {
b[i] = runes[rand.Intn(len(runes))]
}
return string(b)
}