-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
235 lines (217 loc) · 5.93 KB
/
context.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
package actor
import (
"runtime"
"time"
)
// Actor Context
//
// Actor Context will be passed to actor's message handler with message received.
// This data enables message handler to access myself(it's useful when you want
// to send a message to myself), and to change its behavior.
type ActorContext struct {
Self *Actor
monitor *ForwardingActor
originalBehavior Receive
currentBehavior Receive
behaviorStack []Receive
mailbox chan Message
killChan chan kill
attachMonChan chan *Actor
detachMonChan chan *Actor
addChildChan chan *Actor
receiveTimeout time.Duration
prePrecessHook func()
}
// internal Messages accepted by actorContext
type terminate struct{}
type kill struct{}
type shutdown struct{}
// Become change actor's behavior.
//
// ActorContext can be accessed in message handler.
// For example,
// echo := func(msg Message, context *ActorContext){
// fmt.Println(msg)
// context.Become(echoInUpper)
// }
// echoInUpper := func(msg Message, context *ActorContext){
// fmt.Println(msg)
// context.Unbecome()
// }
// alternate := system.Spawn(echo)
// "alternate" actor behaves echo and echoInUpper alternately.
// As you might notice, actor's behavior was stacked. So, you can
// unbecome to set back to the previous behavior.
//
// If discardOld flag is true, this doesn't push a given behavior to
// its behavior stack but just overwrite the top of the stack.
func (context *ActorContext) Become(behavior Receive, discardOld bool) {
if discardOld {
l := len(context.behaviorStack)
context.behaviorStack[l-1] = behavior
} else {
newStack := append(context.behaviorStack, behavior)
context.behaviorStack = newStack
}
context.currentBehavior = behavior
}
// Unbecome set its behavior to the previous behavior in its behavior stack.
//
// Please see Become for details.
func (context *ActorContext) Unbecome() {
l := len(context.behaviorStack)
if l > 1 {
context.behaviorStack = context.behaviorStack[:l-1]
context.currentBehavior = context.behaviorStack[l-2]
} else {
// TODO raise error
}
}
// Watch method attaches myself to given actor as monitor.
//
// This is equivalent with
// actor.Monitor(context.Self)
func (context *ActorContext) Watch(actor *Actor){
actor.Monitor(context.Self)
}
// Unwatch method detaches myself as monitor from given actor.
//
// This is equivalent with
// actor.Demonitor(context.Self)
func (context *ActorContext) Unwatch(actor* Actor){
actor.Demonitor(context.Self)
}
// constructor
func newActorContext(self *Actor, receive Receive) *ActorContext {
// TODO make parameters configurable
context := &ActorContext{
Self: self,
originalBehavior: receive,
currentBehavior: receive,
behaviorStack: []Receive{receive},
mailbox: make(chan Message, 100),
// buffer size for control message is 1 (cotrol method would block)
attachMonChan: make(chan *Actor),
detachMonChan: make(chan *Actor),
killChan: make(chan kill),
addChildChan: make(chan *Actor),
receiveTimeout: time.Duration(10) * time.Millisecond,
}
return context
}
func (context *ActorContext) closeAllChan() {
// TODO flush remained message to deadletter channel??
go func() {
defer logPanic(context.Self)
close(context.mailbox)
close(context.addChildChan)
close(context.attachMonChan)
close(context.detachMonChan)
close(context.killChan)
}()
}
func (context *ActorContext) start() chan bool {
startLatch := make(chan bool)
context.Self.System.wg.Add(1)
go func() {
defer func() {
context.Self.System.running.Remove(context.Self)
context.Self.System.wg.Done()
context.closeAllChan()
context.Self.System.stopped.Add(context.Self)
}()
context.Self.System.running.Add(context.Self)
<-startLatch
close(startLatch)
context.loop()
}()
return startLatch
}
func (context *ActorContext) attachMonitor(mon *Actor) {
context.attachMonChan <- mon
}
func (context *ActorContext) detachMonitor(mon *Actor) {
context.detachMonChan <- mon
}
func (context *ActorContext) kill() {
context.killChan <- kill{}
}
func (context *ActorContext) terminate() {
context.mailbox <- Message{PoisonPill{}}
}
// Actor's main loop which is executed in go routine
func (context *ActorContext) loop() {
// TODO how monitor detect STARTED??
// now monitor can't detect STARTED.
// context.notifyMonitors(Message{STARTED})
for {
// TODO log
receiveTerminated := false
select {
case <-context.killChan:
context.notifyMonitors(Message{Down{
Cause: "killed",
Actor: context.Self,
}})
context.Self.children.Do(func(child *Actor){
if child.IsRunning() {
child.context.kill()
}
})
return
case mon := <-context.attachMonChan:
if context.monitor == nil {
context.monitor = context.Self.System.spawnMonitorForwarderFor(context.Self)
}
context.monitor.Add(mon)
case mon := <-context.detachMonChan:
context.monitor.Remove(mon)
case child := <-context.addChildChan:
context.Self.children.Add(child)
default:
if context.prePrecessHook!= nil {
context.prePrecessHook()
}
receiveTerminated = context.processOneMessage()
}
if receiveTerminated {
return
}
// force give back control to goroutine scheduler.
runtime.Gosched()
}
}
func (context *ActorContext) processOneMessage() bool{
select {
case msg := <-context.mailbox:
if len(msg) == 1 {
if _, ok := msg[0].(PoisonPill); ok {
context.notifyMonitors(Message{Down{
Cause: "terminated",
Actor: context.Self,
}})
context.Self.children.Do(func(child *Actor){
if child.IsRunning() {
child.context.terminate()
}
})
return true
} else {
context.currentBehavior(msg, context)
}
} else {
context.currentBehavior(msg, context)
}
case <-time.After(context.receiveTimeout):
// TODO log
// timeout
}
return false
}
func (context *ActorContext) notifyMonitors(msg Message) {
if context.monitor != nil {
context.monitor.Send(msg)
} else {
//TODO log
}
}