-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbiter.go
241 lines (205 loc) · 4.95 KB
/
arbiter.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
package arbiter
import (
"context"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
)
// Arbiter is tracer to manage lifecycle of goroutines.
type Arbiter struct {
lock chan struct{}
runningCount int32
ctx context.Context
cancelFunc context.CancelFunc
ended uint32
sigFibreExit chan struct{}
sigOS chan os.Signal
children sync.Map
parent *Arbiter
preStop func()
afterStop func()
}
// NewWithParent creates a new arbiter atteched to specified parent arbiter.
// The arbiter will be shut down by the parent or a call to Arbiter.Shutdown().
func NewWithParent(parent *Arbiter) *Arbiter {
a := &Arbiter{
sigFibreExit: make(chan struct{}, 10),
sigOS: make(chan os.Signal, 0),
lock: make(chan struct{}, 1),
parent: parent,
ended: 0,
}
a.lock <- struct{}{}
var parentCtx context.Context
if parent != nil {
parentCtx = parent.ctx
} else {
parentCtx = context.Background()
}
a.ctx, a.cancelFunc = context.WithCancel(parentCtx)
if parent != nil {
select {
case <-a.ctx.Done():
a.ended = 1
default:
}
// join parent.
parent.children.Store(a, struct{}{})
parent.Go(func() {
a.Join()
// Corner case: when a new arbiter is creating with a shutting down parent, `ended` flag may be not correctly set.
// Passively set `ended` flag prevent this case.
atomic.StoreUint32(&a.ended, 1)
parent.children.Delete(a)
})
} else {
go func() {
a.Join()
}()
}
return a
}
// New create a new arbiter.
func New() *Arbiter {
return NewWithParent(nil)
}
// NumGoroutine returns the number of goroutines currently exists on Arbiter tree.
func (a *Arbiter) NumGoroutine() (n int32) {
n = atomic.LoadInt32(&a.runningCount)
if n > 0 {
n -= int32(1 - len(a.lock))
}
a.children.Range(func(k, v interface{}) bool {
n += k.(*Arbiter).NumGoroutine() - 1
return true
})
return
}
// Go spawns the proc (act the same as the "go" keyword) and let the arbiter traces it.
func (a *Arbiter) Go(proc func()) *Arbiter {
atomic.AddInt32(&a.runningCount, 1)
go func() {
defer func() {
a.sigFibreExit <- struct{}{}
}()
proc()
}()
return a
}
// TickGo spawns proc with specified period.
func (a *Arbiter) TickGo(proc func(func(), time.Time), period time.Duration, brust uint32) (cancel func()) {
if brust < 1 {
return
}
var tickCtx context.Context
ticker := time.NewTicker(period)
tickCtx, cancel = context.WithCancel(a.ctx)
a.Go(func() {
defer ticker.Stop()
<-tickCtx.Done()
ticker.Stop()
})
for idx := uint32(0); idx < brust; idx++ {
a.Go(func() {
for {
select {
case t := <-ticker.C:
a.Do(func() { proc(cancel, t.Add(period)) })
case <-tickCtx.Done():
return
}
}
})
}
return
}
// Do calls the proc.
func (a *Arbiter) Do(proc func()) *Arbiter {
atomic.AddInt32(&a.runningCount, 1)
defer func() {
a.sigFibreExit <- struct{}{}
}()
proc()
return a
}
// ShouldRun is called by goroutines traced by Arbiter, indicating whether the goroutines should continue to execute.
func (a *Arbiter) ShouldRun() bool {
return atomic.LoadUint32(&a.ended) < 1
}
// Exit returns a channel that is closed when arbiter is shutdown.
func (a *Arbiter) Exit() <-chan struct{} {
return a.ctx.Done()
}
// Context return context for goroutine control.
func (a *Arbiter) Context() context.Context {
return a.ctx
}
// Shutdown shuts the arbiter, sending exit signal to all goroutines and executions.
func (a *Arbiter) Shutdown() {
a.Do(func() { a.shutdown() })
}
func (a *Arbiter) fastShutdown() {
atomic.StoreUint32(&a.ended, 1)
a.children.Range(func(k, v interface{}) bool {
child := k.(*Arbiter)
child.fastShutdown()
return true
})
}
func (a *Arbiter) shutdown() {
a.fastShutdown()
a.cancelFunc()
}
// StopOSSignals chooses OS signals to shut the arbiter.
func (a *Arbiter) StopOSSignals(stopSignals ...os.Signal) *Arbiter {
signal.Notify(a.sigOS, stopSignals...)
return a
}
// Join waits until all goroutines exited (sync mode).
func (a *Arbiter) Join() {
<-a.lock
defer func() { a.lock <- struct{}{} }()
if a.NumGoroutine() > 0 || a.ShouldRun() {
a.Go(func() {
<-a.Exit()
preStop := a.preStop
if preStop != nil {
preStop()
}
})
c := a.runningCount
for c > 0 {
select {
case <-a.sigFibreExit:
c = atomic.AddInt32(&a.runningCount, -1)
case <-a.sigOS:
if a.ShouldRun() {
a.shutdown()
}
}
}
afterStop := a.afterStop
if afterStop != nil {
afterStop()
}
}
}
// Arbit configures SIGKILL and SIGINT as shutting down signal and waits until all goroutines exited.
func (a *Arbiter) Arbit() error {
a.StopOSSignals(syscall.SIGTERM, syscall.SIGINT)
a.Join()
return nil
}
// HookPreStop inserts pre-stop (a shutdown triggered) callback function.
func (a *Arbiter) HookPreStop(proc func()) *Arbiter {
a.preStop = proc
return a
}
// HookStopped inserts after-stop (all goroutines and executions finished) callback function.
func (a *Arbiter) HookStopped(proc func()) *Arbiter {
a.afterStop = proc
return a
}