forked from liangdas/mqant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
423 lines (380 loc) · 12.3 KB
/
app.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2014 mqant Author. All Rights Reserved.
//
// 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 defaultApp
import (
"encoding/json"
"flag"
"fmt"
"github.com/liangdas/mqant/conf"
"github.com/liangdas/mqant/gate"
"github.com/liangdas/mqant/log"
"github.com/liangdas/mqant/module"
"github.com/liangdas/mqant/module/base"
"github.com/liangdas/mqant/module/modules"
"github.com/liangdas/mqant/rpc"
"github.com/liangdas/mqant/rpc/base"
opentracing "github.com/opentracing/opentracing-go"
"hash/crc32"
"math"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
)
type resultInfo struct {
Error string //错误结果 如果为nil表示请求正确
Result interface{} //结果
}
type protocolMarshalImp struct {
data []byte
}
func (this *protocolMarshalImp) GetData() []byte {
return this.data
}
func NewApp(version string) module.App {
app := new(DefaultApp)
app.routes = map[string]func(app module.App, Type string, hash string) module.ServerSession{}
app.serverList = map[string]module.ServerSession{}
app.defaultRoutes = func(app module.App, Type string, hash string) module.ServerSession {
//默认使用第一个Server
servers := app.GetServersByType(Type)
if len(servers) == 0 {
return nil
}
index := int(math.Abs(float64(crc32.ChecksumIEEE([]byte(hash))))) % len(servers)
return servers[index]
}
app.rpcserializes = map[string]module.RPCSerialize{}
app.version = version
return app
}
type DefaultApp struct {
//module.App
version string
serverList map[string]module.ServerSession
settings conf.Config
processId string
routes map[string]func(app module.App, Type string, hash string) module.ServerSession
defaultRoutes func(app module.App, Type string, hash string) module.ServerSession
//将一个RPC调用路由到新的路由上
mapRoute func(app module.App, route string) string
rpcserializes map[string]module.RPCSerialize
getTracer func() opentracing.Tracer
configurationLoaded func(app module.App)
startup func(app module.App)
moduleInited func(app module.App, module module.Module)
judgeGuest func(session gate.Session) bool
protocolMarshal func(Result interface{}, Error string) (module.ProtocolMarshal, string)
}
func (app *DefaultApp) Run(debug bool, mods ...module.Module) error {
wdPath := flag.String("wd", "", "Server work directory")
confPath := flag.String("conf", "", "Server configuration file path")
ProcessID := flag.String("pid", "development", "Server ProcessID?")
Logdir := flag.String("log", "", "Log file directory?")
flag.Parse() //解析输入的参数
app.processId = *ProcessID
ApplicationDir := ""
if *wdPath != "" {
_, err := os.Open(*wdPath)
if err != nil {
panic(err)
}
os.Chdir(*wdPath)
ApplicationDir, err = os.Getwd()
} else {
var err error
ApplicationDir, err = os.Getwd()
if err != nil {
file, _ := exec.LookPath(os.Args[0])
ApplicationPath, _ := filepath.Abs(file)
ApplicationDir, _ = filepath.Split(ApplicationPath)
}
}
defaultConfPath := fmt.Sprintf("%s/bin/conf/server.json", ApplicationDir)
defaultLogPath := fmt.Sprintf("%s/bin/logs", ApplicationDir)
if *confPath == "" {
*confPath = defaultConfPath
}
if *Logdir == "" {
*Logdir = defaultLogPath
}
f, err := os.Open(*confPath)
if err != nil {
panic(err)
}
_, err = os.Open(*Logdir)
if err != nil {
//文件不存在
err := os.Mkdir(*Logdir, os.ModePerm) //
if err != nil {
fmt.Println(err)
}
}
fmt.Println("Server configuration file path :", *confPath)
conf.LoadConfig(f.Name()) //加载配置文件
app.Configure(conf.Conf) //配置信息
log.InitBeego(debug, *ProcessID, *Logdir, conf.Conf.Log)
log.Info("mqant %v starting up", app.version)
if app.configurationLoaded != nil {
app.configurationLoaded(app)
}
manager := basemodule.NewModuleManager()
manager.RegisterRunMod(modules.TimerModule()) //注册时间轮模块 每一个进程都默认运行
// module
for i := 0; i < len(mods); i++ {
mods[i].OnAppConfigurationLoaded(app)
manager.Register(mods[i])
}
app.OnInit(app.settings)
manager.Init(app, *ProcessID)
if app.startup != nil {
app.startup(app)
}
// close
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
manager.Destroy()
app.OnDestroy()
log.Info("mqant closing down (signal: %v)", sig)
return nil
}
func (app *DefaultApp) Route(moduleType string, fn func(app module.App, Type string, hash string) module.ServerSession) error {
app.routes[moduleType] = fn
return nil
}
func (app *DefaultApp) SetMapRoute(fn func(app module.App, route string) string) error {
app.mapRoute = fn
return nil
}
func (app *DefaultApp) getRoute(moduleType string) func(app module.App, Type string, hash string) module.ServerSession {
fn := app.routes[moduleType]
if fn == nil {
//如果没有设置的路由,则使用默认的
return app.defaultRoutes
}
return fn
}
func (app *DefaultApp) AddRPCSerialize(name string, Interface module.RPCSerialize) error {
if _, ok := app.rpcserializes[name]; ok {
return fmt.Errorf("The name(%s) has been occupied", name)
}
app.rpcserializes[name] = Interface
return nil
}
func (app *DefaultApp) GetRPCSerialize() map[string]module.RPCSerialize {
return app.rpcserializes
}
func (app *DefaultApp) Configure(settings conf.Config) error {
app.settings = settings
return nil
}
/**
*/
func (app *DefaultApp) OnInit(settings conf.Config) error {
app.serverList = make(map[string]module.ServerSession)
for Type, ModuleInfos := range settings.Module {
for _, moduel := range ModuleInfos {
m := app.serverList[moduel.Id]
if m != nil {
//如果Id已经存在,说明有两个相同Id的模块,这种情况不能被允许,这里就直接抛异常 强制崩溃以免以后调试找不到问题
panic(fmt.Sprintf("ServerId (%s) Type (%s) of the modules already exist Can not be reused ServerId (%s) Type (%s)", m.GetId(), m.GetType(), moduel.Id, Type))
}
client, err := defaultrpc.NewRPCClient(app, moduel.Id)
if err != nil {
continue
}
if moduel.Rabbitmq != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRabbitmqClient(moduel.Rabbitmq)
}
if moduel.Redis != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRedisClient(moduel.Redis)
}
if moduel.UDP != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewUdpClient(moduel.UDP)
}
session := basemodule.NewServerSession(moduel.Id, Type, client)
app.serverList[moduel.Id] = session
log.Info("RPCClient create success type(%s) id(%s)", Type, moduel.Id)
}
}
return nil
}
func (app *DefaultApp) OnDestroy() error {
for id, session := range app.serverList {
log.Info("RPCClient closeing type(%s) id(%s)", session.GetType(), id)
err := session.GetRpc().Done()
if err != nil {
log.Warning("RPCClient close fail type(%s) id(%s)", session.GetType(), id)
} else {
log.Info("RPCClient close success type(%s) id(%s)", session.GetType(), id)
}
}
return nil
}
func (app *DefaultApp) RegisterLocalClient(serverId string, server mqrpc.RPCServer) error {
if session, ok := app.serverList[serverId]; ok {
return session.GetRpc().NewLocalClient(server)
} else {
return fmt.Errorf("Server(%s) Not Found", serverId)
}
return nil
}
func (app *DefaultApp) GetServerById(serverId string) (module.ServerSession, error) {
if session, ok := app.serverList[serverId]; ok {
return session, nil
} else {
return nil, fmt.Errorf("Server(%s) Not Found", serverId)
}
}
func (app *DefaultApp) GetServersByType(Type string) []module.ServerSession {
sessions := make([]module.ServerSession, 0)
for _, session := range app.serverList {
if session.GetType() == Type {
sessions = append(sessions, session)
}
}
return sessions
}
func (app *DefaultApp) GetRouteServer(filter string, hash string) (s module.ServerSession, err error) {
if app.mapRoute != nil {
//进行一次路由转换
filter = app.mapRoute(app, filter)
}
sl := strings.Split(filter, "@")
if len(sl) == 2 {
moduleID := sl[1]
if moduleID != "" {
return app.GetServerById(moduleID)
}
}
moduleType := sl[0]
route := app.getRoute(moduleType)
s = route(app, moduleType, hash)
if s == nil {
err = fmt.Errorf("Server(type : %s) Not Found", moduleType)
}
return
}
func (app *DefaultApp) GetSettings() conf.Config {
return app.settings
}
func (app *DefaultApp) GetProcessID() string {
return app.processId
}
func (app *DefaultApp) RpcInvoke(module module.RPCModule, moduleType string, _func string, params ...interface{}) (result interface{}, err string) {
server, e := app.GetRouteServer(moduleType, module.GetServerId())
if e != nil {
err = e.Error()
return
}
return server.Call(_func, params...)
}
func (app *DefaultApp) RpcInvokeNR(module module.RPCModule, moduleType string, _func string, params ...interface{}) (err error) {
server, err := app.GetRouteServer(moduleType, module.GetServerId())
if err != nil {
return
}
return server.CallNR(_func, params...)
}
func (app *DefaultApp) RpcInvokeArgs(module module.RPCModule, moduleType string, _func string, ArgsType []string, args [][]byte) (result interface{}, err string) {
server, e := app.GetRouteServer(moduleType, module.GetServerId())
if e != nil {
err = e.Error()
return
}
return server.CallArgs(_func, ArgsType, args)
}
func (app *DefaultApp) RpcInvokeNRArgs(module module.RPCModule, moduleType string, _func string, ArgsType []string, args [][]byte) (err error) {
server, err := app.GetRouteServer(moduleType, module.GetServerId())
if err != nil {
return
}
return server.CallNRArgs(_func, ArgsType, args)
}
func (app *DefaultApp) RpcInvokeUnreliable(module module.RPCModule, moduleType string, _func string, params ...interface{}) (result interface{}, err string) {
server, e := app.GetRouteServer(moduleType, module.GetServerId())
if e != nil {
err = e.Error()
return
}
return server.CallUnreliable(_func, params...)
}
func (app *DefaultApp) RpcInvokeArgsUnreliable(module module.RPCModule, moduleType string, _func string, ArgsType []string, args [][]byte) (interface{}, string) {
server, err := app.GetRouteServer(moduleType, module.GetServerId())
if err != nil {
return nil, err.Error()
}
return server.CallArgsUnreliable(_func, ArgsType, args)
}
func (app *DefaultApp) DefaultTracer(_func func() opentracing.Tracer) error {
app.getTracer = _func
return nil
}
func (app *DefaultApp) GetTracer() opentracing.Tracer {
if app.getTracer != nil {
return app.getTracer()
}
return nil
}
func (app *DefaultApp) GetModuleInited() func(app module.App, module module.Module) {
return app.moduleInited
}
func (app *DefaultApp) GetJudgeGuest() func(session gate.Session) bool {
return app.judgeGuest
}
func (app *DefaultApp) OnConfigurationLoaded(_func func(app module.App)) error {
app.configurationLoaded = _func
return nil
}
func (app *DefaultApp) OnModuleInited(_func func(app module.App, module module.Module)) error {
app.moduleInited = _func
return nil
}
func (app *DefaultApp) OnStartup(_func func(app module.App)) error {
app.startup = _func
return nil
}
func (app *DefaultApp) SetJudgeGuest(_func func(session gate.Session) bool) error {
app.judgeGuest = _func
return nil
}
func (app *DefaultApp) SetProtocolMarshal(protocolMarshal func(Result interface{}, Error string) (module.ProtocolMarshal, string)) error {
app.protocolMarshal = protocolMarshal
return nil
}
func (app *DefaultApp) ProtocolMarshal(Result interface{}, Error string) (module.ProtocolMarshal, string) {
if app.protocolMarshal != nil {
return app.protocolMarshal(Result, Error)
}
r := &resultInfo{
Error: Error,
Result: Result,
}
b, err := json.Marshal(r)
if err == nil {
return app.NewProtocolMarshal(b), ""
} else {
return nil, err.Error()
}
}
func (app *DefaultApp) NewProtocolMarshal(data []byte) module.ProtocolMarshal {
return &protocolMarshalImp{
data: data,
}
}