-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
mailbox.go
196 lines (167 loc) · 4.5 KB
/
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
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
package actor
import (
"runtime"
"sync/atomic"
"github.com/asynkron/protoactor-go/internal/queue/mpsc"
)
// MailboxMiddleware is an interface for intercepting messages and events in the mailbox
type MailboxMiddleware interface {
MailboxStarted()
MessagePosted(message interface{})
MessageReceived(message interface{})
MailboxEmpty()
}
// MessageInvoker is the interface used by a mailbox to forward messages for processing
type MessageInvoker interface {
InvokeSystemMessage(interface{})
InvokeUserMessage(interface{})
EscalateFailure(reason interface{}, message interface{})
}
// Mailbox interface is used to enqueue messages to the mailbox
type Mailbox interface {
PostUserMessage(message interface{})
PostSystemMessage(message interface{})
RegisterHandlers(invoker MessageInvoker, dispatcher Dispatcher)
Start()
UserMessageCount() int
}
// MailboxProducer is a function which creates a new mailbox
type MailboxProducer func() Mailbox
const (
idle int32 = iota
running
)
type defaultMailbox struct {
userMailbox queue
systemMailbox *mpsc.Queue
schedulerStatus int32
userMessages int32
sysMessages int32
suspended int32
invoker MessageInvoker
dispatcher Dispatcher
middlewares []MailboxMiddleware
}
func (m *defaultMailbox) PostUserMessage(message interface{}) {
// is it a raw batch message?
if batch, ok := message.(MessageBatch); ok {
messages := batch.GetMessages()
for _, msg := range messages {
m.PostUserMessage(msg)
}
}
// is it an envelope batch message?
// FIXME: check if this is still needed, maybe MessageEnvelope can only exist as a pointer
if env, ok := message.(MessageEnvelope); ok {
if batch, ok := env.Message.(MessageBatch); ok {
messages := batch.GetMessages()
for _, msg := range messages {
m.PostUserMessage(msg)
}
}
}
if env, ok := message.(*MessageEnvelope); ok {
if batch, ok := env.Message.(MessageBatch); ok {
messages := batch.GetMessages()
for _, msg := range messages {
m.PostUserMessage(msg)
}
}
}
// normal messages
for _, ms := range m.middlewares {
ms.MessagePosted(message)
}
m.userMailbox.Push(message)
atomic.AddInt32(&m.userMessages, 1)
m.schedule()
}
func (m *defaultMailbox) PostSystemMessage(message interface{}) {
for _, ms := range m.middlewares {
ms.MessagePosted(message)
}
m.systemMailbox.Push(message)
atomic.AddInt32(&m.sysMessages, 1)
m.schedule()
}
func (m *defaultMailbox) RegisterHandlers(invoker MessageInvoker, dispatcher Dispatcher) {
m.invoker = invoker
m.dispatcher = dispatcher
}
func (m *defaultMailbox) schedule() {
if atomic.CompareAndSwapInt32(&m.schedulerStatus, idle, running) {
m.dispatcher.Schedule(m.processMessages)
}
}
func (m *defaultMailbox) processMessages() {
process:
m.run()
// set mailbox to idle
atomic.StoreInt32(&m.schedulerStatus, idle)
sys := atomic.LoadInt32(&m.sysMessages)
user := atomic.LoadInt32(&m.userMessages)
// check if there are still messages to process (sent after the message loop ended)
if sys > 0 || (atomic.LoadInt32(&m.suspended) == 0 && user > 0) {
// try setting the mailbox back to running
if atomic.CompareAndSwapInt32(&m.schedulerStatus, idle, running) {
// fmt.Printf("looping %v %v %v\n", sys, user, m.suspended)
goto process
}
}
for _, ms := range m.middlewares {
ms.MailboxEmpty()
}
}
func (m *defaultMailbox) run() {
var msg interface{}
defer func() {
if r := recover(); r != nil {
m.invoker.EscalateFailure(r, msg)
}
}()
i, t := 0, m.dispatcher.Throughput()
for {
if i > t {
i = 0
runtime.Gosched()
}
i++
// keep processing system messages until queue is empty
if msg = m.systemMailbox.Pop(); msg != nil {
atomic.AddInt32(&m.sysMessages, -1)
switch msg.(type) {
case *SuspendMailbox:
atomic.StoreInt32(&m.suspended, 1)
case *ResumeMailbox:
atomic.StoreInt32(&m.suspended, 0)
default:
m.invoker.InvokeSystemMessage(msg)
}
for _, ms := range m.middlewares {
ms.MessageReceived(msg)
}
continue
}
// didn't process a system message, so break until we are resumed
if atomic.LoadInt32(&m.suspended) == 1 {
return
}
if msg = m.userMailbox.Pop(); msg != nil {
atomic.AddInt32(&m.userMessages, -1)
m.invoker.InvokeUserMessage(msg)
for _, ms := range m.middlewares {
ms.MessageReceived(msg)
}
} else {
return
}
}
}
func (m *defaultMailbox) Start() {
for _, ms := range m.middlewares {
ms.MailboxStarted()
}
}
func (m *defaultMailbox) UserMessageCount() int {
return int(atomic.LoadInt32(&m.userMessages))
}