forked from asynkron/protoactor-go
/
supervision.go
68 lines (56 loc) · 1.86 KB
/
supervision.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
package actor
type Directive int
const (
ResumeDirective Directive = iota
RestartDirective
StopDirective
EscalateDirective
)
type Decider func(child *PID, cause interface{}) Directive
//TODO: as we dont allow remote children or remote SupervisionStrategy
//Instead of letting the parent keep track of child restart stats.
//this info could actually go into each actor, sending it back to the parent as part of the Failure message
type SupervisorStrategy interface {
HandleFailure(supervisor Supervisor, child *PID, cause interface{})
}
type OneForOneStrategy struct {
maxNrOfRetries int
withinTimeRangeMilliseconds int
decider Decider
}
type Supervisor interface {
Children() []*PID
EscalateFailure(who *PID, reason interface{})
}
func (strategy *OneForOneStrategy) HandleFailure(supervisor Supervisor, child *PID, reason interface{}) {
directive := strategy.decider(child, reason)
switch directive {
case ResumeDirective:
//resume the failing child
child.sendSystemMessage(resumeMailboxMessage)
case RestartDirective:
//restart the failing child
child.sendSystemMessage(restartMessage)
case StopDirective:
//stop the failing child
child.Stop()
case EscalateDirective:
//send failure to parent
//supervisor mailbox
supervisor.EscalateFailure(child, reason)
}
}
func NewOneForOneStrategy(maxNrOfRetries int, withinTimeRangeMilliseconds int, decider Decider) SupervisorStrategy {
return &OneForOneStrategy{
maxNrOfRetries: maxNrOfRetries,
withinTimeRangeMilliseconds: withinTimeRangeMilliseconds,
decider: decider,
}
}
func DefaultDecider(child *PID, reason interface{}) Directive {
return RestartDirective
}
var defaultSupervisionStrategy = NewOneForOneStrategy(10, 3000, DefaultDecider)
func DefaultSupervisionStrategy() SupervisorStrategy {
return defaultSupervisionStrategy
}