-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
342 lines (288 loc) · 7.48 KB
/
system.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package tractor
import (
"fmt"
"os"
"sync"
)
const defaultMailboxSize = 1000
const defaultCommandsSize = 2
func Start(root SetupHandler) ActorSystem {
system := &actorSystemImpl{}
system.start(root)
return system
}
type actorSystemImpl struct {
context *localActorContext
root *localActorRef
}
func (system *actorSystemImpl) Context() ActorContext {
return system.context
}
func (system *actorSystemImpl) Wait() {
system.context.childrenWaitGroup.Wait()
}
type localActorRef struct {
context *localActorContext
}
func (ref *localActorRef) Tell(ctx ActorContext, msg interface{}) {
ref.context.mailbox.Tell(envelope{msg: msg, sender: ctx.Self()})
}
type terminateListener struct {
ref ActorRef
msg interface{}
}
type envelope struct {
sender ActorRef
msg interface{}
}
type mailbox struct {
messages chan envelope
commands chan interface{}
stash []envelope
}
func (m *mailbox) takeCommand() interface{} {
select {
case cmd := <-m.commands:
return cmd
default:
return nil
}
}
func (m *mailbox) TellCommand(msg interface{}) {
m.commands <- msg
}
func (m *mailbox) take() interface{} {
if len(m.stash) > 0 {
env := m.stash[0]
m.stash = m.stash[1:]
return env
}
select {
case cmd := <-m.commands:
return cmd
case env := <-m.messages:
return env
}
}
func (m *mailbox) Tell(e envelope) {
m.messages <- e
}
func (m *mailbox) unstashAll(buffer []envelope) {
m.stash = append(m.stash, buffer...)
}
type localActorContext struct {
system *actorSystemImpl
parent *localActorContext
childrenWaitGroup *sync.WaitGroup
self *localActorRef
deliverSignals bool
children []*localActorRef
listeners []terminateListener
currentEnvelope *envelope
mailbox mailbox
}
func (ctx *localActorContext) Ask(ref ActorRef, msg interface{}) chan interface{} {
ch := make(chan interface{}, 1)
ctx.Spawn(func(ctx ActorContext) MessageHandler {
ref.Tell(ctx, msg)
return func(message interface{}) MessageHandler {
ch <- message
return Stopped()
}
})
return ch
}
func (ctx *localActorContext) Sender() ActorRef {
return ctx.currentEnvelope.sender
}
func (ctx *localActorContext) Watch(actor ActorRef) {
ctx.WatchWith(actor, Terminated{})
}
func (ctx *localActorContext) WatchWith(actor ActorRef, msg interface{}) {
actor.(*localActorRef).context.mailbox.TellCommand(&listenCommand{ref: ctx.self, msg: msg})
}
func (ctx *localActorContext) Children() []ActorRef {
result := make([]ActorRef, len(ctx.children))
for i, ref := range ctx.children {
result[i] = ref
}
return result
}
func newContext(system *actorSystemImpl, self *localActorRef, parent *localActorContext) *localActorContext {
return &localActorContext{
system: system,
self: self,
parent: parent,
childrenWaitGroup: &sync.WaitGroup{},
mailbox: newMailbox(),
}
}
func newMailbox() mailbox {
return mailbox{
messages: make(chan envelope, defaultMailboxSize),
commands: make(chan interface{}, defaultCommandsSize),
}
}
func (ctx *localActorContext) Parent() ActorRef {
return ctx.parent.self
}
func (ctx *localActorContext) Self() ActorRef {
return ctx.self
}
func (ctx *localActorContext) DeliverSignals(value bool) {
ctx.deliverSignals = value
}
func (ctx *localActorContext) Spawn(handler SetupHandler) ActorRef {
return ctx.spawn(handler)
}
func (ctx *localActorContext) spawn(handler SetupHandler) *localActorRef {
ref := &localActorRef{}
childContext := newContext(ctx.system, ref, ctx)
ref.context = childContext
ctx.children = append(ctx.children, ref)
ctx.childrenWaitGroup.Add(1)
go func() {
childContext.mainLoop(handler)
}()
return ref
}
type terminateCommand struct{}
type listenCommand struct {
ref ActorRef
msg interface{}
}
type childTerminatedCommand struct {
ref ActorRef
}
func (ctx *localActorContext) mainLoop(setup SetupHandler) {
messageHandler := ctx.setup(setup)
if messageHandler == nil {
messageHandler = Stopped()
}
lastMessageHandler := messageHandler
if ctx.deliverSignals && !isStopped(messageHandler) {
if newHandler := ctx.deliver(messageHandler, PostInitSignal{}); newHandler != nil {
messageHandler = newHandler
}
}
for {
if isStopped(messageHandler) {
break
}
lastMessageHandler = messageHandler
msg := ctx.mailbox.take()
switch command := msg.(type) {
case envelope:
ctx.currentEnvelope = &command
if newHandler := ctx.deliver(messageHandler, command.msg); newHandler != nil {
messageHandler = newHandler
}
ctx.currentEnvelope = nil
case *terminateCommand:
messageHandler = Stopped()
case *listenCommand:
ctx.onListenCommand(command)
case *childTerminatedCommand:
ctx.onChildTerminatedCommand(command)
default:
panic(fmt.Sprintf("Bad command: %T", command))
}
}
// drain commands if any
l:
for {
cmd := ctx.mailbox.takeCommand()
if cmd == nil {
break l
}
switch command := cmd.(type) {
case *terminateCommand:
// do nothing
case *listenCommand:
ctx.onListenCommand(command)
case *childTerminatedCommand:
ctx.onChildTerminatedCommand(command)
default:
panic(fmt.Sprintf("Bad command: %T", cmd))
}
}
if ctx.deliverSignals {
ctx.deliver(lastMessageHandler, PreStopSignal{})
}
for _, child := range ctx.children {
child.context.mailbox.TellCommand(&terminateCommand{})
}
ctx.childrenWaitGroup.Wait()
if ctx.deliverSignals {
ctx.deliver(lastMessageHandler, PostStopSignal{})
}
ctx.parent.childrenWaitGroup.Done()
if ctx.parent.self != nil {
ctx.parent.mailbox.TellCommand(&childTerminatedCommand{ref: ctx.self})
}
for _, listener := range ctx.listeners {
listener.ref.Tell(ctx, listener.msg)
}
}
func (ctx *localActorContext) setup(handler SetupHandler) MessageHandler {
defer func() {
if err := recover(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "actor setup panic: %s\n", err)
}
}()
return handler(ctx)
}
func (ctx *localActorContext) deliver(messageHandler MessageHandler, msg interface{}) (newHandler MessageHandler) {
newHandler = Stopped()
defer func() {
if err := recover(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "actor panic: %s\n", err)
}
}()
newHandler = messageHandler(msg)
return newHandler
}
func (ctx *localActorContext) onListenCommand(command *listenCommand) {
ctx.listeners = append(ctx.listeners, terminateListener{ref: command.ref, msg: command.msg})
}
func (ctx *localActorContext) onChildTerminatedCommand(command *childTerminatedCommand) {
for i, ref := range ctx.children {
if ref == command.ref {
ctx.children = append(ctx.children[:i], ctx.children[i+1:]...)
return
}
}
panic(fmt.Sprintf("bad child: %+v %+v", ctx.children, command))
}
func (system *actorSystemImpl) Root() ActorRef {
return system.root
}
func (system *actorSystemImpl) start(root SetupHandler) {
system.context = newContext(system, nil, nil)
system.root = system.context.spawn(root)
}
type stashBuffer struct {
context *localActorContext
buffer []envelope
}
func (s *stashBuffer) Stash(msg interface{}) {
s.buffer = append(s.buffer, envelope{
sender: s.context.currentEnvelope.sender,
msg: msg,
})
}
func (s *stashBuffer) UnstashAll(handler MessageHandler) MessageHandler {
s.context.mailbox.unstashAll(s.buffer)
s.buffer = nil
return handler
}
func (s *stashBuffer) Unstash(handler MessageHandler, count int) MessageHandler {
s.context.mailbox.unstashAll(s.buffer[:count])
s.buffer = s.buffer[count:]
return handler
}
func (ctx *localActorContext) NewStash(size int) StashBuffer {
return &stashBuffer{
context: ctx,
}
}