-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactor.go
More file actions
72 lines (61 loc) · 1.36 KB
/
actor.go
File metadata and controls
72 lines (61 loc) · 1.36 KB
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
package actor
type Inbox chan Message
type Message func(a Actor) error
type Actor interface {
Inbox() Inbox
CreatorInbox() Inbox
SpawnNested(Actor, string) (Inbox, error)
Initialize() error
IsStopping() bool
Finalize()
HandleError(error) error
HandleLastMsg(Actor, error) error
ID() string
initialize(Inbox, string, bool) Inbox
finalize()
stop(error)
okToStop() bool
registerNested(Actor)
stopAllNested()
unregisterNested(Inbox) Actor
}
func run(a Actor) {
for !a.okToStop() {
err := (<-a.Inbox())(a)
if err != nil {
errWhileHandling := a.HandleError(err)
if errWhileHandling != nil {
a.stop(errWhileHandling)
}
}
}
}
func initialize(a Actor, creatorInbox Inbox, id string, asRoot bool) (Inbox, error) {
ibox := a.initialize(creatorInbox, id, asRoot)
err := a.Initialize()
if err != nil {
return nil, err
}
return ibox, nil
}
func finalize(a Actor) {
a.Finalize()
a.finalize()
}
func launch(a Actor, creatorInbox Inbox, id string, asRoot bool) (Inbox, error) {
ibox, err := initialize(a, creatorInbox, id, asRoot)
if err != nil {
finalize(a)
return nil, err
}
go func() {
defer finalize(a)
run(a)
}()
return ibox, nil
}
func SpawnRoot(a Actor, id string) (creatorInbox, nestedInbox Inbox, err error) {
creatorInbox = make(Inbox, 1) // Only meant to be closed
nestedInbox, err = launch(a, creatorInbox, id, true)
return
}