/
super.go
188 lines (160 loc) · 4.45 KB
/
super.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
package actor
import (
"sync"
"sync/atomic"
"time"
)
type VFOnStarted func(ctx Context)
type VFOnStopping func(ctx Context)
type VFOnStopped func(ctx Context)
type VFOnReceiveMessage func(ctx Context)
type VFOnActorTerminated func(who *PID, ctx Context)
type VFOnRestarting func(ctx Context)
type VFOnRestarted func(ctx Context)
type Super struct {
vfOnStarted VFOnStarted
vfOnStopping VFOnStopping
vfOnStopped VFOnStopped
vfOnReceiveMessage VFOnReceiveMessage
vfOnActorTerminated VFOnActorTerminated
vfOnRestarting VFOnRestarting
vfOnRestarted VFOnRestarted
PID *PID
ParentPID *PID
restarting bool
startedWg *sync.WaitGroup
stoppedWg *sync.WaitGroup
stoppedWgOnce sync.Once
timerMgr *TimerManager
stopFlag int32
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func NewSuper(
vfOnStarted VFOnStarted,
vfOnStopping VFOnStopping,
vfOnStopped VFOnStopped,
vfOnReceiveMessage VFOnReceiveMessage,
vfOnActorTerminated VFOnActorTerminated,
vfOnRestarting VFOnRestarting,
vfOnRestarted VFOnRestarted,
startedWg *sync.WaitGroup,
stoppedWg *sync.WaitGroup,
) *Super {
s := &Super{
vfOnStarted: vfOnStarted,
vfOnStopping: vfOnStopping,
vfOnStopped: vfOnStopped,
vfOnReceiveMessage: vfOnReceiveMessage,
vfOnActorTerminated: vfOnActorTerminated,
vfOnRestarting: vfOnRestarting,
vfOnRestarted: vfOnRestarted,
startedWg: startedWg,
stoppedWg: stoppedWg,
stopFlag: 0,
}
if s.vfOnStarted == nil {
s.vfOnStarted = func(ctx Context) {}
}
if s.vfOnStopping == nil {
s.vfOnStopping = func(ctx Context) {}
}
if s.vfOnStopped == nil {
s.vfOnStopped = func(ctx Context) {}
}
if s.vfOnReceiveMessage == nil {
s.vfOnReceiveMessage = func(ctx Context) {}
}
if s.vfOnActorTerminated == nil {
s.vfOnActorTerminated = func(who *PID, ctx Context) {}
}
if s.vfOnRestarting == nil {
s.vfOnRestarting = func(ctx Context) {}
}
if s.vfOnRestarted == nil {
s.vfOnRestarted = func(ctx Context) {}
}
if s.startedWg != nil {
s.startedWg.Add(1)
}
return s
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (s *Super) Receive(ctx Context) {
switch msg := ctx.Message().(type) {
case *Started:
if !s.restarting {
s.PID = ctx.Self()
s.ParentPID = ctx.Parent()
s.timerMgr = NewTimerManager(s.PID)
s.vfOnStarted(ctx)
if s.startedWg != nil {
s.startedWg.Done()
}
if s.stoppedWg != nil {
s.stoppedWg.Add(1)
}
logger.Debug("[%s] started", s.PID.Id)
} else {
logger.Error("[%s] restarted", s.PID.Id)
s.restarting = false
s.vfOnRestarted(ctx)
}
case *Stopping:
logger.Debug("[%s] stopping", s.PID.Id)
s.vfOnStopping(ctx)
case *Stopped:
logger.Debug("[%s] stopped", s.PID.Id)
s.vfOnStopped(ctx)
s.timerMgr.StopAll()
if s.stoppedWg != nil {
s.stoppedWgOnce.Do(func() {
// 避免重复Done, 导致panic
s.stoppedWg.Done()
})
}
case *Terminated:
s.vfOnActorTerminated(msg.Who, ctx)
case *Restarting:
logger.Error("[%s] restarting", s.PID.Id)
s.restarting = true
s.vfOnRestarting(ctx)
case *timeout:
if err := s.timerMgr.Trigger(msg.ID); err != nil {
logger.Error("[%s] trigger time fail, %s", s.PID.Id, err)
}
default:
s.vfOnReceiveMessage(ctx)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (s Super) Sign() string {
return s.PID.Id
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type ErrActorStopped struct {
}
func (ErrActorStopped) Error() string {
return "actor stopped"
}
func (s *Super) IsStopped() bool {
return atomic.LoadInt32(&s.stopFlag) != 0
}
func (s *Super) Stop() error {
if !atomic.CompareAndSwapInt32(&s.stopFlag, 0, 1) {
return &ErrActorStopped{}
}
if s.PID != nil {
StopActor(s.PID)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (s *Super) NewTimer(dur time.Duration, cb TimerCallback) TimerID {
return s.timerMgr.NewTimer(dur, cb)
}
func (s *Super) NewLoopTimer(interval time.Duration, cb TimerCallback) TimerID {
return s.timerMgr.NewLoopTimer(interval, cb)
}
func (s *Super) StopTimer(id TimerID) error {
return s.timerMgr.Stop(id)
}