-
Notifications
You must be signed in to change notification settings - Fork 1
/
forward.go
52 lines (44 loc) · 1.09 KB
/
forward.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 actor
import "github.com/dropbox/godropbox/container/set"
type ForwardingActor struct {
*Actor
addRecipientChan chan addRecipient
delRecipientChan chan removeRecipient
recipients set.Set
}
// internal message used in ForwardingActor
type addRecipient struct {
recipient *Actor
}
type removeRecipient struct {
recipient *Actor
}
func (actor *ForwardingActor) Add(recipient *Actor) {
actor.addRecipientChan <- addRecipient{recipient: recipient}
}
func (actor *ForwardingActor) Remove(recipient *Actor) {
actor.delRecipientChan <- removeRecipient{recipient: recipient}
}
func (actor *ForwardingActor) preProcessHook() func(){
return func() {
select{
case m := <-actor.addRecipientChan:
actor.recipients.Add(m.recipient)
case m := <-actor.delRecipientChan:
actor.recipients.Remove(m.recipient)
default:
}
}
}
func (actor *ForwardingActor) receive() Receive {
return func(msg Message, context *ActorContext) {
actor.recipients.Do(func(e interface{}) {
if a, ok := e.(*Actor); ok {
a.Send(msg)
}
if a, ok := e.(*ForwardingActor); ok {
a.Send(msg)
}
})
}
}