-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper.go
254 lines (208 loc) · 6.88 KB
/
keeper.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package shutdownKeeper
import (
"context"
"os"
"os/signal"
"sync"
"sync/atomic"
"time"
)
// HoldToken is used by subroutines to listen for shutdown events. It allows subroutines to complete their work.
// Each subroutine that holding a HoldToken should call the Release() method after it finishes its work.
// Once all HoldTokens are released, the shutdown keeper will return from its Wait() method call.
type HoldToken interface {
// ListenShutdown will block the current goroutine until the shutdown stage is triggered.
ListenShutdown()
Release()
Context() context.Context
}
type TokenAllocator interface {
AllocHoldToken() HoldToken
OnShuttingDown(func())
}
const (
_ int32 = iota
statusReady
statusWaiting
statusShutting
statusShutdown
)
// KeeperOpts contains options for creating a ShutdownKeeper.
type KeeperOpts struct {
// Signals specifies the signals that ShutdownKeeper will listen for (for example, syscall.SIGINT, syscall.SIGTERM).
// Receiving any signal from this list will trigger the shutdown process.
Signals []os.Signal
// OnSignalShutdown is called when ShutdownKeeper receives any signal provided by Signals.
OnSignalShutdown func(os.Signal)
// Context is used to listen for the context.Done() event, which will trigger the shutdown process.
Context context.Context
// OnContextDone is called when ShutdownKeeper receives a context.Done() event.
OnContextDone func()
// MaxHoldTime is the maximum time that ShutdownKeeper will wait for all HoldTokens to be released when shutdown process is triggered.
// If the time is exceeded, ShutdownKeeper.Wait() will force return.
// The default value of MaxHoldTime is 30 seconds.
MaxHoldTime time.Duration
// If ForceHold is true, ShutdownKeeper will always hold the shutdown process for MaxHoldTime, even if no HoldToken is allocated or all the HoldTokens are released.
ForceHold bool
}
// ShutdownKeeper manages the graceful shutdown process of a program.
type ShutdownKeeper struct {
signals []os.Signal
signalHandler func(os.Signal)
signalChan chan os.Signal
ctx context.Context
ctxDoneHandler func()
maxHoldTime time.Duration
forceHold bool
holdingTokenNum int32
shutdownHoldChan chan struct{}
closeShutdownHoldChan func()
shuttingChan chan struct{}
status int32
shutdownChan chan struct{}
tokenCtx context.Context
tokenCtxCancel func()
}
func NewKeeper(opts KeeperOpts) *ShutdownKeeper {
maxHoldTime := opts.MaxHoldTime
if maxHoldTime == 0 {
maxHoldTime = 30 * time.Second
}
ctx, cancel := context.WithCancel(context.Background())
keeper := &ShutdownKeeper{
signals: opts.Signals,
signalHandler: opts.OnSignalShutdown,
signalChan: make(chan os.Signal, 1),
ctx: opts.Context,
ctxDoneHandler: opts.OnContextDone,
maxHoldTime: maxHoldTime,
forceHold: opts.ForceHold,
holdingTokenNum: 0,
shutdownHoldChan: make(chan struct{}),
shuttingChan: make(chan struct{}),
status: statusReady,
shutdownChan: make(chan struct{}),
tokenCtx: ctx,
tokenCtxCancel: cancel,
}
keeper.closeShutdownHoldChan = sync.OnceFunc(func() {
close(keeper.shutdownHoldChan)
})
return keeper
}
// Wait blocks the current goroutine until the shutdown process is finished.
// It listens to Signals and Context if provided.
// Once any of them is triggered, the graceful shutdown process will be performed.
// If the ShutdownKeeper is already in shutdown status, Wait will return immediately.
func (k *ShutdownKeeper) Wait() {
if !atomic.CompareAndSwapInt32(&k.status, statusReady, statusWaiting) {
return
}
if len(k.signals) == 0 && k.ctx == nil && k.getHoldingTokenNum() == 0 {
k.startShutdown(nil)
} else {
go k.listenSignals()
go k.listenContext()
}
<-k.shuttingChan
k.tokenCtxCancel()
if k.forceHold {
<-time.After(k.maxHoldTime)
} else if k.getHoldingTokenNum() > 0 {
select {
case <-time.After(k.maxHoldTime):
case <-k.shutdownHoldChan:
}
}
k.closeShutdownHoldChan()
atomic.StoreInt32(&k.status, statusShutdown)
close(k.shutdownChan)
}
// AllocHoldToken allocates a hold token.
func (k *ShutdownKeeper) AllocHoldToken() HoldToken {
atomic.AddInt32(&k.holdingTokenNum, 1)
return newHoldTokenImpl(func() {
if atomic.AddInt32(&k.holdingTokenNum, -1) == 0 {
s := atomic.LoadInt32(&k.status)
if s == statusWaiting || s == statusShutting {
k.closeShutdownHoldChan()
k.startShutdown(nil)
}
}
}, k.tokenCtx)
}
// OnShuttingDown registers a function to be called when the shutdown process is triggered.
func (k *ShutdownKeeper) OnShuttingDown(f func()) {
s := atomic.LoadInt32(&k.status)
if s != statusReady && s != statusWaiting {
return
}
go func(token HoldToken) {
defer token.Release()
token.ListenShutdown()
f()
}(k.AllocHoldToken())
}
func (k *ShutdownKeeper) listenSignals() {
if len(k.signals) == 0 {
return
}
signal.Notify(k.signalChan, k.signals...)
loop:
for {
select {
case s := <-k.signalChan:
k.startShutdown(nil)
if k.signalHandler != nil {
k.signalHandler(s)
}
case <-k.shutdownChan:
break loop
}
}
signal.Stop(k.signalChan)
close(k.signalChan)
}
func (k *ShutdownKeeper) listenContext() {
if k.ctx == nil {
return
}
select {
case <-k.ctx.Done():
k.startShutdown(k.ctxDoneHandler)
case <-k.shutdownChan:
}
}
func (k *ShutdownKeeper) startShutdown(eventFunc func()) bool {
if atomic.CompareAndSwapInt32(&k.status, statusWaiting, statusShutting) || atomic.CompareAndSwapInt32(&k.status, statusReady, statusShutting) {
defer close(k.shuttingChan)
if eventFunc != nil {
eventFunc()
}
return true
}
return false
}
// getHoldingTokenNum returns the number of hold tokens that have not been released yet.
func (k *ShutdownKeeper) getHoldingTokenNum() int32 {
return atomic.LoadInt32(&k.holdingTokenNum)
}
type holdTokenImpl struct {
releasingFunc func()
ctx context.Context
}
func newHoldTokenImpl(releasingFunc func(), ctx context.Context) *holdTokenImpl {
return &holdTokenImpl{
releasingFunc: sync.OnceFunc(releasingFunc),
ctx: ctx,
}
}
func (kt *holdTokenImpl) ListenShutdown() {
<-kt.Context().Done()
}
func (kt *holdTokenImpl) Release() {
kt.releasingFunc()
}
func (kt *holdTokenImpl) Context() context.Context {
return kt.ctx
}