-
Notifications
You must be signed in to change notification settings - Fork 0
/
imq_mailbox.go
144 lines (117 loc) · 2.76 KB
/
imq_mailbox.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
package uwe
const (
TargetBroadcast = "*"
TargetSelfInit = "self-init"
)
type (
Mailbox interface {
SenderBus
ReaderBus
}
SenderBus interface {
Send(target WorkerName, data interface{})
SendWithKind(target WorkerName, kind MessageKind, data interface{})
SendToMany(kind MessageKind, data interface{}, targets ...WorkerName)
SelfInit(name WorkerName) Mailbox
}
ReaderBus interface {
Messages() <-chan *Message
}
MessageKind int
Message struct {
Target WorkerName
Sender WorkerName
Kind MessageKind
Data interface{}
}
)
type eventBus struct {
name WorkerName
readOnly bool
writeOnly bool
// in is channel for incoming messages for a worker
in chan *Message
// out is channel for outgoing messages from a worker
out chan<- *Message
}
func NewSenderBus(fromWorker chan<- *Message) SenderBus {
in := make(chan *Message, 2)
return &eventBus{
writeOnly: true,
name: "",
in: in,
out: fromWorker,
}
}
func NewReaderBus(name WorkerName, toWorker chan *Message) ReaderBus {
return &eventBus{
readOnly: true,
name: name,
in: toWorker,
}
}
func NewBus(name WorkerName, toWorker, fromWorker chan *Message) Mailbox {
return &eventBus{
name: name,
in: toWorker,
out: fromWorker,
}
}
func (wc *eventBus) SendWithKind(target WorkerName, kind MessageKind, data interface{}) {
if wc.readOnly {
return
}
wc.out <- &Message{
Target: target,
Sender: wc.name,
Kind: kind,
Data: data,
}
}
func (wc *eventBus) SendToMany(kind MessageKind, data interface{}, targets ...WorkerName) {
if wc.readOnly {
return
}
for _, target := range targets {
wc.out <- &Message{
Target: target,
Sender: wc.name,
Kind: kind,
Data: data,
}
}
}
func (wc *eventBus) Send(target WorkerName, data interface{}) {
if wc.readOnly {
return
}
wc.out <- &Message{
Target: target,
Sender: wc.name,
Data: data,
}
}
func (wc *eventBus) SelfInit(name WorkerName) Mailbox {
wc.name = name
wc.writeOnly = false
wc.out <- &Message{
Target: TargetSelfInit,
Sender: wc.name,
Data: wc.in,
}
return wc
}
func (wc *eventBus) Messages() <-chan *Message { return wc.in }
// NopMailbox is an empty Mailbox
type NopMailbox struct{}
func (*NopMailbox) Send(WorkerName, interface{}) {}
func (*NopMailbox) SendWithKind(WorkerName, MessageKind, interface{}) {}
func (*NopMailbox) SendToMany(MessageKind, interface{}, ...WorkerName) {}
func (m *NopMailbox) SelfInit(WorkerName) Mailbox { return m }
func (*NopMailbox) Messages() <-chan *Message {
c := make(chan *Message)
// TODO: Requires a decision whether to return an empty channel or return a closed channel.
// An empty channel results in blocking the reader forever.
// close(c)
return c
}