-
Notifications
You must be signed in to change notification settings - Fork 3
/
runtime.go
251 lines (232 loc) · 7.23 KB
/
runtime.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package framework
import (
"git.golaxy.org/core"
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/service"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/framework/plugins/dent"
"git.golaxy.org/framework/plugins/log"
"git.golaxy.org/framework/plugins/log/zap_log"
"github.com/spf13/viper"
etcdv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"sync"
)
type _IRuntimeGeneric interface {
setup(ctx service.Context, composite any)
generate(settings _RuntimeSettings) core.Runtime
}
type _RuntimeSettings struct {
Name string
AutoRecover bool
ReportError chan error
FPS float32
AutoRun bool
ProcessQueueCapacity int
}
// RuntimeGeneric 运行时泛化类型
type RuntimeGeneric struct {
servCtx service.Context
composite any
}
func (r *RuntimeGeneric) setup(ctx service.Context, composite any) {
r.servCtx = ctx
r.composite = composite
}
func (r *RuntimeGeneric) generate(settings _RuntimeSettings) core.Runtime {
startupConf := r.GetStartupConf()
face := iface.Face[runtime.Context]{}
if cb, ok := r.composite.(IRuntimeInstantiation); ok {
face = iface.MakeFace(cb.Instantiation())
}
iFrameLoopBeginCB, _ := face.Iface.(LifecycleRuntimeFrameLoopBegin)
iFrameUpdateBeginCB, _ := face.Iface.(LifecycleRuntimeFrameUpdateBegin)
iFrameUpdateEndCB, _ := face.Iface.(LifecycleRuntimeFrameUpdateEnd)
iFrameLoopEndCB, _ := face.Iface.(LifecycleRuntimeFrameLoopEnd)
iRunCallBeginCB, _ := face.Iface.(LifecycleRuntimeRunCallBegin)
iRunCallEndCB, _ := face.Iface.(LifecycleRuntimeRunCallEnd)
iRunGCBeginCB, _ := face.Iface.(LifecycleRuntimeRunGCBegin)
iRunGCEndCB, _ := face.Iface.(LifecycleRuntimeRunGCEnd)
frameLoopBeginCB, _ := r.composite.(LifecycleRuntimeFrameLoopBegin)
frameUpdateBeginCB, _ := r.composite.(LifecycleRuntimeFrameUpdateBegin)
frameUpdateEndCB, _ := r.composite.(LifecycleRuntimeFrameUpdateEnd)
frameLoopEndCB, _ := r.composite.(LifecycleRuntimeFrameLoopEnd)
runCallBeginCB, _ := r.composite.(LifecycleRuntimeRunCallBegin)
runCallEndCB, _ := r.composite.(LifecycleRuntimeRunCallEnd)
runGCBeginCB, _ := r.composite.(LifecycleRuntimeRunGCBegin)
runGCEndCB, _ := r.composite.(LifecycleRuntimeRunGCEnd)
rtCtx := runtime.NewContext(r.GetServiceCtx(),
runtime.With.Context.CompositeFace(face),
runtime.With.Context.Name(settings.Name),
runtime.With.Context.PanicHandling(settings.AutoRecover, settings.ReportError),
runtime.With.Context.RunningHandler(generic.MakeDelegateAction2(func(ctx runtime.Context, state runtime.RunningState) {
switch state {
case runtime.RunningState_Birth:
if cb, ok := r.composite.(LifecycleRuntimeBirth); ok {
cb.Birth(ctx)
}
if cb, ok := ctx.(LifecycleRuntimeBirth); ok {
cb.Birth(ctx)
}
case runtime.RunningState_Starting:
if cb, ok := r.composite.(LifecycleRuntimeStarting); ok {
cb.Starting(ctx)
}
if cb, ok := ctx.(LifecycleRuntimeStarting); ok {
cb.Starting(ctx)
}
case runtime.RunningState_Started:
if cb, ok := r.composite.(LifecycleRuntimeStarted); ok {
cb.Started(ctx)
}
if cb, ok := ctx.(LifecycleRuntimeStarted); ok {
cb.Started(ctx)
}
case runtime.RunningState_FrameLoopBegin:
if cb := frameLoopBeginCB; cb != nil {
cb.FrameLoopBegin(ctx)
}
if cb := iFrameLoopBeginCB; cb != nil {
cb.FrameLoopBegin(ctx)
}
case runtime.RunningState_FrameUpdateBegin:
if cb := frameUpdateBeginCB; cb != nil {
cb.FrameUpdateBegin(ctx)
}
if cb := iFrameUpdateBeginCB; cb != nil {
cb.FrameUpdateBegin(ctx)
}
case runtime.RunningState_FrameUpdateEnd:
if cb := frameUpdateEndCB; cb != nil {
cb.FrameUpdateEnd(ctx)
}
if cb := iFrameUpdateEndCB; cb != nil {
cb.FrameUpdateEnd(ctx)
}
case runtime.RunningState_FrameLoopEnd:
if cb := frameLoopEndCB; cb != nil {
cb.FrameLoopEnd(ctx)
}
if cb := iFrameLoopEndCB; cb != nil {
cb.FrameLoopEnd(ctx)
}
case runtime.RunningState_RunCallBegin:
if cb := runCallBeginCB; cb != nil {
cb.RunCallBegin(ctx)
}
if cb := iRunCallBeginCB; cb != nil {
cb.RunCallBegin(ctx)
}
case runtime.RunningState_RunCallEnd:
if cb := runCallEndCB; cb != nil {
cb.RunCallEnd(ctx)
}
if cb := iRunCallEndCB; cb != nil {
cb.RunCallEnd(ctx)
}
case runtime.RunningState_RunGCBegin:
if cb := runGCBeginCB; cb != nil {
cb.RunGCBegin(ctx)
}
if cb := iRunGCBeginCB; cb != nil {
cb.RunGCBegin(ctx)
}
case runtime.RunningState_RunGCEnd:
if cb := runGCEndCB; cb != nil {
cb.RunGCEnd(ctx)
}
if cb := iRunGCEndCB; cb != nil {
cb.RunGCEnd(ctx)
}
case runtime.RunningState_Terminating:
if cb, ok := r.composite.(LifecycleRuntimeTerminating); ok {
cb.Terminating(ctx)
}
if cb, ok := ctx.(LifecycleRuntimeTerminating); ok {
cb.Terminating(ctx)
}
case runtime.RunningState_Terminated:
if cb, ok := r.composite.(LifecycleRuntimeTerminated); ok {
cb.Terminated(ctx)
}
if cb, ok := ctx.(LifecycleRuntimeTerminated); ok {
cb.Terminated(ctx)
}
}
})),
)
// 安装日志插件
if cb, ok := r.composite.(InstallRuntimeLogger); ok {
cb.InstallLogger(rtCtx)
}
if _, ok := rtCtx.GetPluginBundle().Get(log.Name); !ok {
if v, _ := r.GetMemKVs().Load("zap.logger"); v != nil {
zap_log.Install(rtCtx,
zap_log.With.ZapLogger(v.(*zap.Logger)),
zap_log.With.ServiceInfo(startupConf.GetBool("log.service_info")),
zap_log.With.RuntimeInfo(startupConf.GetBool("log.runtime_info")),
)
}
}
// 安装分布式实体支持插件
if cb, ok := r.composite.(InstallRuntimeDistEntities); ok {
cb.InstallDistEntities(rtCtx)
}
if _, ok := rtCtx.GetPluginBundle().Get(dent.Name); !ok {
v, _ := r.GetMemKVs().Load("etcd.init_client")
fun, _ := v.(func())
if fun == nil {
panic("service memory etcd.init_client not existed")
}
fun()
v, _ = r.GetMemKVs().Load("etcd.client")
cli, _ := v.(*etcdv3.Client)
if cli == nil {
panic("service memory etcd.client not existed")
}
dent.Install(rtCtx,
dent.With.EtcdClient(cli),
dent.With.TTL(startupConf.GetDuration("service.dent_ttl")),
)
}
// 组装完成回调回调
if cb, ok := r.composite.(LifecycleRuntimeBuilt); ok {
cb.Built(rtCtx)
}
if cb, ok := face.Iface.(LifecycleRuntimeBuilt); ok {
cb.Built(rtCtx)
}
return core.NewRuntime(rtCtx,
core.With.Runtime.Frame(func() runtime.Frame {
if settings.FPS <= 0 {
return nil
}
return runtime.NewFrame(
runtime.With.Frame.TargetFPS(settings.FPS),
)
}()),
core.With.Runtime.AutoRun(settings.AutoRun),
core.With.Runtime.ProcessQueueCapacity(settings.ProcessQueueCapacity),
)
}
// GetServiceCtx 获取服务上下文
func (r *RuntimeGeneric) GetServiceCtx() service.Context {
return r.servCtx
}
// GetStartupConf 获取启动参数配置
func (r *RuntimeGeneric) GetStartupConf() *viper.Viper {
v, _ := r.GetMemKVs().Load("startup.conf")
if v == nil {
panic("service memory startup.conf not existed")
}
return v.(*viper.Viper)
}
// GetMemKVs 获取服务内存KV数据库
func (r *RuntimeGeneric) GetMemKVs() *sync.Map {
memKVs, _ := r.GetServiceCtx().Value("mem_kvs").(*sync.Map)
if memKVs == nil {
panic("service memory not existed")
}
return memKVs
}