/
actor_system.go
179 lines (155 loc) · 3.97 KB
/
actor_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
package actor
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"github.com/gogo/protobuf/proto"
"github.com/wwj31/godactor/actor/err"
"github.com/wwj31/godactor/actor/internal/actor_msg"
"github.com/wwj31/godactor/tools"
)
/* 所有actor的驱动器和调度器*/
type SystemOption func(*ActorSystem) error
type ActorSystem struct {
actorAddr string // 远程actor连接端口
waitStop *sync.WaitGroup // stop wait
exiting int32 // 停止标记
// 本地actor管理
actorCache sync.Map // 所有本地actor
waitRun chan *actor // 等待启动的actor列表
//集群管理actorId
clusterId string
// 辅助模块
cmd ICmd
*EventDispatcher
}
func System(op ...SystemOption) (*ActorSystem, error) {
sys := &ActorSystem{
waitStop: &sync.WaitGroup{},
waitRun: make(chan *actor, 100),
}
sys.EventDispatcher = NewActorEvent(sys)
for _, f := range op {
if e := f(sys); e != nil {
return nil, fmt.Errorf("%w %w", err.ActorSystemOptionErr, e.Error())
}
}
for len(sys.waitRun) > 0 {
cluster := <-sys.waitRun
wait := make(chan struct{})
sys.runActor(cluster, wait)
<-wait
}
tools.GoEngine(func() {
for {
select {
case actor, ok := <-sys.waitRun:
if !ok {
return
}
sys.runActor(actor, nil)
}
}
})
logger.Info("ActorSystem Start")
return sys, nil
}
func (s *ActorSystem) waitCluster() {
for {
continueWait := false
s.actorCache.Range(func(key, value interface{}) bool {
if continueWait = key != s.clusterId; continueWait {
return false
}
return true
})
if !continueWait {
return
}
runtime.Gosched()
}
}
func (s *ActorSystem) Address() string {
return s.actorAddr
}
func (s *ActorSystem) SetCluster(id string) {
s.clusterId = id
}
func (s *ActorSystem) Stop() {
if atomic.CompareAndSwapInt32(&s.exiting, 0, 1) {
//shutdown() 通知所有actor执行关闭
s.actorCache.Range(func(key, value interface{}) bool {
value.(*actor).SystemStop()
return true
})
s.waitCluster()
s.Send("", s.clusterId, "", "stop")
s.waitStop.Wait()
close(s.waitRun)
logger.Info("ActorSystem Exit")
}
}
// 注册actor,外部创建对象,保证ActorId唯一性
func (s *ActorSystem) Regist(actor *actor) error {
if atomic.LoadInt32(&s.exiting) == 1 && !actor.isWaitActor() {
return fmt.Errorf("%w actor:%v", err.RegisterActorSystemErr, actor.GetID())
}
actor.setSystem(s)
if _, has := s.actorCache.LoadOrStore(actor.GetID(), actor); has {
return fmt.Errorf("%w actor:%v", err.RegisterActorSameIdErr, actor.GetID())
}
s.waitStop.Add(1)
tools.Try(
func() { s.waitRun <- actor },
func(ex interface{}) {
s.waitStop.Done()
s.actorCache.Delete(actor.GetID())
})
return nil
}
func (s *ActorSystem) runActor(actor *actor, ok chan struct{}) {
if atomic.LoadInt32(&s.exiting) == 1 && !actor.isWaitActor() {
return
}
go func() {
actor.run(ok)
// exit
logger.KV("actor", actor.GetID()).Info("actor done")
s.actorCache.Delete(actor.GetID())
s.waitStop.Done()
}()
}
// actor之间发送消息,
// sourceid 发送源actor
// targetid 目标actor
// message 消息内容
func (s *ActorSystem) Send(sourceId, targetId, requestId string, msg interface{}) error {
var atr *actor
if localActor, ok := s.actorCache.Load(targetId); ok { //消息能否发给本地
atr = localActor.(*actor)
} else {
_, canRemote := msg.(proto.Message)
cluster, ok := s.actorCache.Load(s.clusterId)
if canRemote && ok { // 消息能否发送给远端
atr = cluster.(*actor)
}
}
if atr == nil {
return fmt.Errorf("%w s[%v] t[%v],r[%v]", err.ActorNotFoundErr, sourceId, targetId, requestId)
}
if e := atr.push(actor_msg.NewLocalActorMessage(sourceId, targetId, requestId, msg)); e != nil {
return fmt.Errorf("%w s[%v] t[%v],r[%v]", e, sourceId, targetId, requestId)
}
return nil
}
func (s *ActorSystem) ClusterId() string {
return s.clusterId
}
// 设置Actor监听的端口
func Addr(addr string) SystemOption {
return func(system *ActorSystem) error {
system.actorAddr = addr
return nil
}
}