forked from kandoo/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
276 lines (239 loc) · 4.51 KB
/
msg.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package beehive
import (
"encoding/gob"
"fmt"
"reflect"
"runtime"
)
// Msg is a generic interface for messages emitted in the system. Messages
// are defined for each type.
type Msg interface {
// Type of the data in this message.
Type() string
// Data stored in the message.
Data() interface{}
// From returns the ID of the sender of this message.
From() uint64
// To returns the ID of the receiver of this message.
To() uint64
// NoReply returns whether we can reply to the message.
NoReply() bool
// IsBroadCast returns whether the message is a broadcast.
IsBroadCast() bool
// IsUnicast returns whether the message is a unicast.
IsUnicast() bool
}
// Typed is a message data with an explicit type.
type Typed interface {
Type() string
}
type msg struct {
MsgData interface{}
MsgFrom uint64
MsgTo uint64
}
func (m msg) NoReply() bool {
return m.MsgFrom == 0
}
func (m msg) IsBroadCast() bool {
return m.MsgTo == 0
}
func (m msg) IsUnicast() bool {
return m.MsgTo != 0
}
func (m msg) Type() string {
return MsgType(m.MsgData)
}
func (m msg) Data() interface{} {
return m.MsgData
}
func (m msg) To() uint64 {
return m.MsgTo
}
func (m msg) From() uint64 {
return m.MsgFrom
}
func (m msg) String() string {
if m.Data() == nil {
return fmt.Sprintf("%v -> %v\t(nil)", m.From(), m.To())
}
return fmt.Sprintf("%v -> %v\t%v(%#v)", m.From(), m.To(), m.Type(), m.Data())
}
// MsgType returns the message type for d.
func MsgType(d interface{}) string {
if t, ok := d.(Typed); ok {
return t.Type()
}
return reflect.TypeOf(d).String()
}
func newMsgFromData(data interface{}, from uint64, to uint64) *msg {
return &msg{
MsgData: data,
MsgFrom: from,
MsgTo: to,
}
}
type msgAndHandler struct {
msg *msg
handler Handler
}
type Emitter interface {
Emit(msgData interface{})
}
func init() {
gob.Register(msg{})
}
type msgChannel struct {
chin chan msgAndHandler
chout chan msgAndHandler
buf []msgAndHandler
start int
end int
}
func newMsgChannel(bufSize uint) *msgChannel {
q := &msgChannel{
chin: make(chan msgAndHandler, bufSize),
chout: make(chan msgAndHandler, bufSize),
buf: make([]msgAndHandler, bufSize),
}
go q.pipe()
return q
}
func (q *msgChannel) pipe() {
var chout chan msgAndHandler
var first msgAndHandler
dequed := false
for {
if dequed {
chout = q.chout
} else {
q.maybeFastPipe()
chout = nil
}
select {
case mh := <-q.chin:
q.enque(mh)
q.maybeReadMore()
if dequed == false {
first, dequed = q.deque()
}
case chout <- first:
q.maybeWriteMore()
first, dequed = q.deque()
}
}
}
func (q *msgChannel) maybeFastPipe() {
cw := cap(q.chout)
cr := cap(q.chin)
for {
// calling cap and len is expensive. cache the values here and update later
// if needed.
w := cw - len(q.chout)
if w == 0 {
if len(q.chin) != cr {
// give it another chance.
runtime.Gosched()
continue
}
return
}
for i := 0; i < w; i++ {
mh := <-q.chin
q.chout <- mh
}
}
}
func (q *msgChannel) maybeReadMore() {
l := len(q.chin)
if l < cap(q.chin) {
return
}
for ; l > 0; l-- {
select {
case mh := <-q.chin:
q.enque(mh)
default:
return
}
}
}
func (q *msgChannel) maybeWriteMore() {
w := cap(q.chout) - len(q.chout)
if w == 0 {
return
}
l := q.len()
if w < l {
l = w
}
for ; l > 0; l-- {
select {
case q.chout <- q.buf[q.start]:
q.deque()
default:
return
}
}
}
func (q *msgChannel) in() chan<- msgAndHandler {
return q.chin
}
func (q *msgChannel) out() <-chan msgAndHandler {
return q.chout
}
func (q *msgChannel) empty() bool {
return q.len() == 0
}
func (q *msgChannel) full() bool {
return q.len() == len(q.buf)-1
}
func (q *msgChannel) enque(mh msgAndHandler) {
if q.full() {
q.maybeExpand()
}
q.buf[q.end] = mh
q.end++
if q.end >= len(q.buf) {
q.end = 0
}
}
func (q *msgChannel) deque() (msgAndHandler, bool) {
if q.empty() {
return msgAndHandler{}, false
}
mh := q.buf[q.start]
q.buf[q.start].msg = nil
q.start++
if q.start >= len(q.buf) {
q.start = 0
}
return mh, true
}
func (q *msgChannel) len() int {
l := q.end - q.start
if l >= 0 {
return l
}
return len(q.buf) + l
}
func (q *msgChannel) free() int {
return len(q.buf) - q.len()
}
func (q *msgChannel) maybeExpand() {
if !q.full() {
return
}
qlen := q.len()
buf := make([]msgAndHandler, len(q.buf)*2)
if q.start < q.end {
copy(buf, q.buf[q.start:q.end])
} else {
l := len(q.buf) - q.start
copy(buf, q.buf[q.start:])
copy(buf[l:], q.buf[:q.end])
}
q.start = 0
q.end = qlen
q.buf = buf
}