-
Notifications
You must be signed in to change notification settings - Fork 2
/
supervisions.go
327 lines (276 loc) · 9.06 KB
/
supervisions.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package actorkit
import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
)
// Decider defines a function which giving a value will return a directive.
type Decider func(interface{}) Directive
// DelayProvider defines a function which giving a int value representing
// increasing attempts, will return an appropriate duration.
type DelayProvider func(int) time.Duration
//*****************************************************************
// AllForOneSupervisor
//*****************************************************************
// PanicAction defines a function type which embodies the action to
// be done with panic'ed value.
type PanicAction func(interface{}, Addr, Actor)
// AllForOneSupervisor implements a one-to-one supervising strategy for giving actors.
type AllForOneSupervisor struct {
Max int
Decider Decider
PanicAction PanicAction
Delay DelayProvider
Invoker SupervisionInvoker
failedRestarts int64
work sync.Mutex
}
// Handle implements the Supervisor interface and provides the algorithm logic for the
// all-for-one monitoring strategy, where a failed actor causes the same effect to be applied
// to all siblings and parent.
func (on *AllForOneSupervisor) Handle(err interface{}, targetAddr Addr, target Actor, parent Actor) {
on.work.Lock()
defer on.work.Unlock()
switch on.Decider(err) {
case PanicDirective:
linearDoUntil(parent.KillChildren, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedKill(err, target.Stats(), targetAddr, target)
}
if on.PanicAction != nil {
on.PanicAction(err, targetAddr, target)
return
}
switch tm := err.(type) {
case PanicEvent:
panic(fmt.Sprintf("%#q\n", tm))
default:
panic(err)
}
case KillDirective:
linearDoUntil(parent.KillChildren, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedKill(err, target.Stats(), targetAddr, target)
}
case StopDirective:
linearDoUntil(parent.StopChildren, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedStop(err, target.Stats(), targetAddr, target)
}
case RestartDirective:
failed := int(atomic.LoadInt64(&on.failedRestarts))
if on.Max > 0 && failed >= on.Max {
return
}
restartErr := target.Restart()
if on.Invoker != nil {
on.Invoker.InvokedRestart(err, target.Stats(), targetAddr, target)
}
if restartErr != nil {
newFailed := atomic.AddInt64(&on.failedRestarts, 1)
if on.Delay != nil {
time.AfterFunc(on.Delay(int(newFailed)), func() {
on.Handle(err, targetAddr, target, parent)
})
return
}
on.Handle(err, targetAddr, target, parent)
return
}
atomic.StoreInt64(&on.failedRestarts, 0)
case DestroyDirective:
linearDoUntil(parent.DestroyChildren, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedDestroy(err, target.Stats(), targetAddr, target)
}
case EscalateDirective:
parent.Escalate(err, targetAddr)
case IgnoreDirective:
return
}
}
//*****************************************************************
// OneForOneSupervisor
//*****************************************************************
// OneForOneSupervisor implements a one-to-one supervising strategy for giving actors.
type OneForOneSupervisor struct {
Max int
Delay DelayProvider
Decider Decider
PanicAction PanicAction
Invoker SupervisionInvoker
failedRestarts int64
work sync.Mutex
}
// Handle implements the Supervisor interface and provides the algorithm logic for the
// one-for-one monitoring strategy, where a failed actor is dealt with singularly without affecting
// it's siblings.
func (on *OneForOneSupervisor) Handle(err interface{}, targetAddr Addr, target Actor, parent Actor) {
on.work.Lock()
defer on.work.Unlock()
switch on.Decider(err) {
case PanicDirective:
linearDoUntil(target.Kill, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedKill(err, target.Stats(), targetAddr, target)
}
if on.PanicAction != nil {
on.PanicAction(err, targetAddr, target)
return
}
switch tm := err.(type) {
case PanicEvent:
panic(fmt.Sprintf("%#q\n", tm))
default:
panic(err)
}
case KillDirective:
linearDoUntil(target.Kill, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedKill(err, target.Stats(), targetAddr, target)
}
case StopDirective:
linearDoUntil(target.Stop, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedStop(err, target.Stats(), targetAddr, target)
}
case RestartDirective:
failed := int(atomic.LoadInt64(&on.failedRestarts))
if on.Max > 0 && failed >= on.Max {
return
}
restartErr := target.Restart()
if on.Invoker != nil {
on.Invoker.InvokedRestart(err, target.Stats(), targetAddr, target)
}
if restartErr != nil {
newFailed := atomic.AddInt64(&on.failedRestarts, 1)
if on.Delay != nil {
time.AfterFunc(on.Delay(int(newFailed)), func() {
on.Handle(err, targetAddr, target, parent)
})
return
}
on.Handle(err, targetAddr, target, parent)
return
}
atomic.StoreInt64(&on.failedRestarts, 0)
case DestroyDirective:
linearDoUntil(target.Destroy, 100, time.Second)
if on.Invoker != nil {
on.Invoker.InvokedDestroy(err, target.Stats(), targetAddr, target)
}
case EscalateDirective:
parent.Escalate(err, targetAddr)
case IgnoreDirective:
return
}
}
//*****************************************************************
// RestartingSupervisor
//*****************************************************************
// RestartingSupervisor implements a one-to-one supervising strategy for giving actors.
type RestartingSupervisor struct {
Delay DelayProvider
Invoker SupervisionInvoker
work sync.Mutex
attempts int
}
// Handle implements a restarting supervision strategy where any escalated error will lead to
// a restart of actor.
func (sp *RestartingSupervisor) Handle(err interface{}, targetAddr Addr, target Actor, parent Actor) {
sp.work.Lock()
defer sp.work.Unlock()
restartErr := target.Restart()
if sp.Invoker != nil {
sp.Invoker.InvokedRestart(err, target.Stats(), targetAddr, target)
}
if restartErr == nil {
sp.attempts = 0
return
}
sp.attempts++
if sp.Delay != nil {
time.AfterFunc(sp.Delay(sp.attempts), func() {
sp.Handle(err, targetAddr, target, parent)
})
return
}
sp.Handle(err, targetAddr, target, parent)
}
//*****************************************************************
// ExponentialBackOffRestartStrategy
//*****************************************************************
// ExponentialBackOffRestartStrategy returns a new ExponentialBackOffSupervisor which will attempt to restart target actor
// where error occurred. If restart fail, it will continuously attempt till it has maxed out chances.
func ExponentialBackOffRestartStrategy(max int, backoff time.Duration, invoker SupervisionInvoker) *ExponentialBackOffSupervisor {
return &ExponentialBackOffSupervisor{
Max: max,
Backoff: backoff,
Invoker: invoker,
Action: func(err interface{}, targetAddr Addr, target Actor, parent Actor) error {
return target.Restart()
},
}
}
// ExponentialBackOffStopStrategy returns a new ExponentialBackOffSupervisor which will attempt to stop target actor
// where error occurred. If restart fail, it will continuously attempt till it has maxed out chances.
func ExponentialBackOffStopStrategy(max int, backoff time.Duration, invoker SupervisionInvoker) *ExponentialBackOffSupervisor {
return &ExponentialBackOffSupervisor{
Max: max,
Backoff: backoff,
Invoker: invoker,
Action: func(err interface{}, targetAddr Addr, target Actor, parent Actor) error {
return target.Stop()
},
}
}
//*****************************************************************
// ExponentialBackOffStrategy
//*****************************************************************
// ExponentialBackOffSupervisor implements a supervisor which will attempt to
// exponentially run a giving action function continuously with an increasing
// backoff time, until it's maximum tries is reached.
type ExponentialBackOffSupervisor struct {
Max int
Backoff time.Duration
Invoker SupervisionInvoker
Action func(err interface{}, targetAddr Addr, target Actor, parent Actor) error
failed int64
work sync.Mutex
}
// Handle implements the exponential restart of giving target actor within giving maximum allowed runs.
func (sp *ExponentialBackOffSupervisor) Handle(err interface{}, targetAddr Addr, target Actor, parent Actor) {
sp.work.Lock()
defer sp.work.Unlock()
failed := atomic.LoadInt64(&sp.failed)
if int(failed) >= sp.Max {
target.Stop()
if sp.Invoker != nil {
sp.Invoker.InvokedStop(err, target.Stats(), targetAddr, target)
}
return
}
var backoff int64
if failed > 0 {
backoff = failed * sp.Backoff.Nanoseconds()
} else {
backoff = sp.Backoff.Nanoseconds()
}
noise := rand.Int63n(500)
dur := time.Duration(backoff + noise)
time.AfterFunc(dur, func() {
actionErr := sp.Action(err, targetAddr, target, parent)
if sp.Invoker != nil {
sp.Invoker.InvokedRestart(err, target.Stats(), targetAddr, target)
}
if actionErr != nil {
atomic.AddInt64(&sp.failed, 1)
sp.Handle(err, targetAddr, target, parent)
return
}
atomic.StoreInt64(&sp.failed, 0)
})
}