-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.go
231 lines (209 loc) · 4.47 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
// Copyright (c) 2016-2023, Jan Cajthaml <jan.cajthaml@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package actorsystem
import (
"context"
"fmt"
)
// ProcessMessage is a function signature definition for remote message processing
type ProcessMessage func(msg string, to Coordinates, from Coordinates)
// System provides support for graceful shutdown
type System struct {
Name string
actors *actorsMap
onMessage ProcessMessage
push Pusher
sub Subber
ctx context.Context
cancel context.CancelFunc
}
// New returns new actor system fascade
func New(name string, lakeHostname string) (System, error) {
if lakeHostname == "" {
return System{}, fmt.Errorf("invalid lake hostname")
}
if name == "" {
return System{}, fmt.Errorf("invalid system name")
}
ctx, cancel := context.WithCancel(context.Background())
return System{
Name: name,
ctx: ctx,
cancel: cancel,
actors: &actorsMap{
underlying: make(map[string]*Actor),
},
onMessage: func(msg string, to Coordinates, from Coordinates) {},
push: NewPusher(lakeHostname),
sub: NewSubber(lakeHostname, name),
}, nil
}
// RegisterOnMessage register callback on message receive
func (s *System) RegisterOnMessage(f ProcessMessage) {
if s == nil {
return
}
s.onMessage = f
}
// RegisterActor register new actor into actor system
func (s *System) RegisterActor(ref *Actor) error {
if s == nil || ref == nil {
return nil
}
_, exists := s.actors.Load(ref.Name)
if exists {
return fmt.Errorf("already exists")
}
s.actors.Store(ref.Name, ref)
go func() {
for {
select {
case p := <-ref.Backlog:
ref.Receive(p)
case <-ref.Exit:
return
}
}
}()
return nil
}
// ActorOf return actor reference by name
func (s *System) ActorOf(name string) (*Actor, error) {
if s == nil {
return nil, fmt.Errorf("cannot call method on nil reference")
}
ref, exists := s.actors.Load(name)
if !exists {
return nil, fmt.Errorf("actor %v not registered", name)
}
return ref, nil
}
// UnregisterActor stops actor and removes it from actor system
func (s *System) UnregisterActor(name string) {
if s == nil {
return
}
ref := s.actors.Delete(name)
if ref == nil {
return
}
close(ref.Exit)
}
// SendMessage send message to to local of remote actor system
func (s *System) SendMessage(msg string, to Coordinates, from Coordinates) {
if s == nil {
return
}
if to.Region == from.Region {
s.onMessage(msg, to, from)
} else {
s.push.Data <- (to.Region + " " + from.Region + " " + to.Name + " " + from.Name + " " + msg)
}
}
func (s *System) processMesage(message string) {
start := 0
end := len(message)
if end == 0 {
return
}
parts := make([]string, 5)
idx := 0
i := 0
for i < end && idx < 4 {
if message[i] == 32 {
if !(start == i && message[start] == 32) {
parts[idx] = message[start:i]
idx++
}
start = i + 1
}
i++
}
if idx < 5 && message[start] != 32 && len(message[start:]) > 0 {
parts[idx] = message[start:]
idx++
}
if idx != 5 {
return
}
s.onMessage(
parts[4],
Coordinates{
Name: parts[2],
Region: parts[0],
},
Coordinates{
Name: parts[3],
Region: parts[1],
},
)
}
func (s *System) exhaustMailbox() {
if s == nil {
return
}
var message string
loop:
select {
case <-s.ctx.Done():
goto eos
case message = <-s.sub.Data:
go s.processMesage(message)
}
goto loop
eos:
return
}
// Stop terminates work
func (s *System) Stop() {
if s == nil {
return
}
s.cancel()
<-s.ctx.Done()
}
// Start spins PUSH and SUB workers
func (s *System) Start() {
if s == nil {
return
}
go func() {
s.sub.Start()
s.Stop()
}()
go func() {
s.push.Start()
s.Stop()
}()
defer func() {
done := make(chan interface{})
go func() {
s.sub.Stop()
close(done)
}()
for {
select {
case <-s.sub.Data:
continue
case <-done:
s.push.Stop()
for actorName := range s.actors.underlying {
s.UnregisterActor(actorName)
}
return
}
}
}()
s.exhaustMailbox()
}