forked from asynkron/protoactor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeractor.go
51 lines (42 loc) · 999 Bytes
/
routeractor.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
package router
import "github.com/AsynkronIT/protoactor-go/actor"
type routerActor struct {
props actor.Props
config RouterConfig
state Interface
}
func (a *routerActor) Receive(context actor.Context) {
switch m := context.Message().(type) {
case *actor.Started:
a.config.OnStarted(context, a.props, a.state)
case *AddRoutee:
r := a.state.GetRoutees()
if r.Contains(m.PID) {
return
}
context.Watch(m.PID)
r.Add(m.PID)
a.state.SetRoutees(r)
case *RemoveRoutee:
r := a.state.GetRoutees()
if !r.Contains(m.PID) {
return
}
context.Unwatch(m.PID)
r.Remove(m.PID)
a.state.SetRoutees(r)
case *BroadcastMessage:
msg := m.Message
sender := context.Sender()
a.state.GetRoutees().ForEach(func(i int, pid actor.PID) {
pid.Request(msg, sender)
})
case *GetRoutees:
r := a.state.GetRoutees()
routees := make([]*actor.PID, r.Len())
r.ForEach(func(i int, pid actor.PID) {
routees[i] = &pid
})
context.Sender().Tell(&Routees{routees})
}
}