-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathactor.go
191 lines (150 loc) · 4.01 KB
/
actor.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
package fpgo
import (
"fmt"
"time"
)
var ErrActorAskTimeout = fmt.Errorf("ErrActorAskTimeout")
// ActorHandle A target could send messages
type ActorHandle interface {
Send(message interface{})
}
// ActorDef Actor model inspired by Erlang/Akka
type ActorDef struct {
id time.Time
isClosed bool
ch chan interface{}
effect func(*ActorDef, interface{})
context map[string]interface{}
children map[time.Time]*ActorDef
parent *ActorDef
}
var defaultActor *ActorDef
// GetDefault Get Default Actor
func (actorSelf *ActorDef) GetDefault() *ActorDef {
return defaultActor
}
// New New Actor instance
func (actorSelf *ActorDef) New(effect func(*ActorDef, interface{})) *ActorDef {
return actorSelf.NewByOptions(effect, make(chan interface{}), map[string]interface{}{})
}
// NewByOptions New Actor by its options
func (actorSelf *ActorDef) NewByOptions(effect func(*ActorDef, interface{}), ioCh chan interface{}, context map[string]interface{}) *ActorDef {
newOne := ActorDef{
id: time.Now(),
ch: ioCh,
effect: effect,
context: context,
children: map[time.Time]*ActorDef{},
}
go newOne.run()
return &newOne
}
// Send Send a message to the Actor
func (actorSelf *ActorDef) Send(message interface{}) {
if actorSelf.isClosed {
return
}
actorSelf.ch <- message
}
// Spawn Spawn a new Actor with parent(this actor)
func (actorSelf *ActorDef) Spawn(effect func(*ActorDef, interface{})) *ActorDef {
newOne := Actor.New(effect)
if actorSelf.isClosed {
return newOne
}
newOne.parent = actorSelf
actorSelf.children[newOne.id] = newOne
return newOne
}
// GetChild Get a child Actor by ID
func (actorSelf *ActorDef) GetChild(id time.Time) *ActorDef {
return actorSelf.children[id]
}
// GetParent Get its parent Actor
func (actorSelf *ActorDef) GetParent() *ActorDef {
return actorSelf.parent
}
// GetID Get ID time.Time
func (actorSelf *ActorDef) GetID() time.Time {
return actorSelf.id
}
// Close Close the Actor
func (actorSelf *ActorDef) Close() {
actorSelf.isClosed = true
close(actorSelf.ch)
}
// IsClosed Check is Closed
func (actorSelf *ActorDef) IsClosed() bool {
return actorSelf.isClosed
}
func (actorSelf *ActorDef) run() {
for message := range actorSelf.ch {
actorSelf.effect(actorSelf, message)
}
}
// Actor Actor utils instance
var Actor ActorDef
// AskDef Ask inspired by Erlang/Akka
type AskDef struct {
id time.Time
ch chan interface{}
Message interface{}
}
// New New Ask instance
func (askSelf *AskDef) New(message interface{}) *AskDef {
return AskNewGenerics(message)
}
// NewByOptions New Ask by its options
func (askSelf *AskDef) NewByOptions(message interface{}, ioCh chan interface{}) *AskDef {
return AskNewByOptionsGenerics(message, ioCh)
}
// AskNewGenerics New Ask instance
func AskNewGenerics(message interface{}) *AskDef {
return AskNewByOptionsGenerics(message, make(chan interface{}))
}
// AskNewByOptionsGenerics New Ask by its options
func AskNewByOptionsGenerics(message interface{}, ioCh chan interface{}) *AskDef {
newOne := AskDef{
id: time.Now(),
ch: ioCh,
Message: message,
}
return &newOne
}
// AskOnce Sender Ask
func (askSelf *AskDef) AskOnce(target ActorHandle) interface{} {
ch := askSelf.AskChannel(target)
defer close(ch)
// var err error
return <-ch
}
// AskOnce Sender Ask with timeout
func (askSelf *AskDef) AskOnceWithTimeout(target ActorHandle, timeout time.Duration) (interface{}, error) {
ch := askSelf.AskChannel(target)
defer close(ch)
var result interface{}
select {
case result = <-ch:
case <-time.After(timeout):
return result, ErrActorAskTimeout
}
return result, nil
}
// AskChannel Sender Ask
func (askSelf *AskDef) AskChannel(target ActorHandle) chan interface{} {
target.Send(askSelf)
return askSelf.ch
}
// Reply Receiver Reply
func (askSelf *AskDef) Reply(response interface{}) {
askSelf.ch <- response
}
// Ask Ask utils instance
var Ask AskDef
func init() {
// Ask = *Ask.New(0, nil)
// Actor = *Actor.New(func(_ *ActorDef, _ interface{}) {})
// Actor.Close()
Actor.isClosed = true
defaultActor = &Actor
}