forked from asynkron/protoactor-go
/
guardian.go
82 lines (65 loc) · 1.9 KB
/
guardian.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
package actor
import (
"errors"
"sync"
"github.com/AsynkronIT/protoactor-go/log"
)
type guardiansValue struct {
guardians *sync.Map
}
var guardians = &guardiansValue{&sync.Map{}}
func (gs *guardiansValue) getGuardianPid(s SupervisorStrategy) *PID {
if g, ok := gs.guardians.Load(s); ok {
return g.(*guardianProcess).pid
}
g := gs.newGuardian(s)
gs.guardians.Store(s, g)
return g.pid
}
// newGuardian creates and returns a new actor.guardianProcess with a timeout of duration d
func (gs *guardiansValue) newGuardian(s SupervisorStrategy) *guardianProcess {
ref := &guardianProcess{strategy: s}
id := ProcessRegistry.NextId()
pid, ok := ProcessRegistry.Add(ref, "guardian"+id)
if !ok {
plog.Error("failed to register guardian process", log.Stringer("pid", pid))
}
ref.pid = pid
return ref
}
type guardianProcess struct {
pid *PID
strategy SupervisorStrategy
}
func (g *guardianProcess) SendUserMessage(pid *PID, message interface{}) {
panic(errors.New("Guardian actor cannot receive any user messages"))
}
func (g *guardianProcess) SendSystemMessage(pid *PID, message interface{}) {
if msg, ok := message.(*Failure); ok {
g.strategy.HandleFailure(g, msg.Who, msg.RestartStats, msg.Reason, msg.Message)
}
}
func (g *guardianProcess) Stop(pid *PID) {
//Ignore
}
func (g *guardianProcess) Children() []*PID {
panic(errors.New("Guardian does not hold its children PIDs"))
}
func (*guardianProcess) EscalateFailure(reason interface{}, message interface{}) {
panic(errors.New("Guardian cannot escalate failure"))
}
func (*guardianProcess) RestartChildren(pids ...*PID) {
for _, pid := range pids {
pid.sendSystemMessage(restartMessage)
}
}
func (*guardianProcess) StopChildren(pids ...*PID) {
for _, pid := range pids {
pid.sendSystemMessage(stopMessage)
}
}
func (*guardianProcess) ResumeChildren(pids ...*PID) {
for _, pid := range pids {
pid.sendSystemMessage(resumeMailboxMessage)
}
}