-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
517 lines (451 loc) · 16.1 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright (c) nano Author and TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.xxxxxxxsssssa
package pitaya
import (
"context"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"time"
"github.com/golang/protobuf/proto"
"github.com/long12310225/pitayaN/v2/acceptor"
"github.com/long12310225/pitayaN/v2/cluster"
"github.com/long12310225/pitayaN/v2/component"
"github.com/long12310225/pitayaN/v2/config"
"github.com/long12310225/pitayaN/v2/conn/message"
"github.com/long12310225/pitayaN/v2/constants"
pcontext "github.com/long12310225/pitayaN/v2/context"
"github.com/long12310225/pitayaN/v2/docgenerator"
"github.com/long12310225/pitayaN/v2/errors"
"github.com/long12310225/pitayaN/v2/groups"
"github.com/long12310225/pitayaN/v2/interfaces"
"github.com/long12310225/pitayaN/v2/logger"
logging "github.com/long12310225/pitayaN/v2/logger/interfaces"
"github.com/long12310225/pitayaN/v2/metrics"
mods "github.com/long12310225/pitayaN/v2/modules"
"github.com/long12310225/pitayaN/v2/remote"
"github.com/long12310225/pitayaN/v2/router"
"github.com/long12310225/pitayaN/v2/serialize"
"github.com/long12310225/pitayaN/v2/service"
"github.com/long12310225/pitayaN/v2/session"
"github.com/long12310225/pitayaN/v2/timer"
"github.com/long12310225/pitayaN/v2/tracing"
"github.com/long12310225/pitayaN/v2/worker"
opentracing "github.com/opentracing/opentracing-go"
)
// ServerMode represents a server mode
type ServerMode byte
const (
_ ServerMode = iota
// Cluster represents a server running with connection to other servers
Cluster
// Standalone represents a server running without connection to other servers
Standalone
)
// Pitaya App interface
type Pitaya interface {
//定制接口
GetRemoteService() *service.RemoteService
GetDieChan() chan bool
SetDebug(debug bool)
SetHeartbeatTime(interval time.Duration)
GetServerID() string
GetMetricsReporters() []metrics.Reporter
GetServer() *cluster.Server
GetServerByID(id string) (*cluster.Server, error)
GetServersByType(t string) (map[string]*cluster.Server, error)
GetServers() []*cluster.Server
GetSessionFromCtx(ctx context.Context) session.Session
Start()
SetDictionary(dict map[string]uint16) error
AddRoute(serverType string, routingFunction router.RoutingFunc) error
Shutdown()
StartWorker()
RegisterRPCJob(rpcJob worker.RPCJob) error
Documentation(getPtrNames bool) (map[string]interface{}, error)
IsRunning() bool
RPC(ctx context.Context, routeStr string, reply proto.Message, arg proto.Message) error
RPCTo(ctx context.Context, serverID, routeStr string, reply proto.Message, arg proto.Message) error
ReliableRPC(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
) (jid string, err error)
ReliableRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *config.EnqueueOpts,
) (jid string, err error)
SendPushToUsers(route string, v interface{}, uids []string, frontendType string) ([]string, error)
SendKickToUsers(uids []string, frontendType string) ([]string, error)
GroupCreate(ctx context.Context, groupName string) error
GroupCreateWithTTL(ctx context.Context, groupName string, ttlTime time.Duration) error
GroupMembers(ctx context.Context, groupName string) ([]string, error)
GroupBroadcast(ctx context.Context, frontendType, groupName, route string, v interface{}) error
GroupContainsMember(ctx context.Context, groupName, uid string) (bool, error)
GroupAddMember(ctx context.Context, groupName, uid string) error
GroupRemoveMember(ctx context.Context, groupName, uid string) error
GroupRemoveAll(ctx context.Context, groupName string) error
GroupCountMembers(ctx context.Context, groupName string) (int, error)
GroupRenewTTL(ctx context.Context, groupName string) error
GroupDelete(ctx context.Context, groupName string) error
Register(c component.Component, options ...component.Option)
RegisterRemote(c component.Component, options ...component.Option)
RegisterModule(module interfaces.Module, name string) error
RegisterModuleAfter(module interfaces.Module, name string) error
RegisterModuleBefore(module interfaces.Module, name string) error
GetModule(name string) (interfaces.Module, error)
}
// App is the base app struct
type App struct {
acceptors []acceptor.Acceptor
config config.PitayaConfig
debug bool
dieChan chan bool
heartbeat time.Duration
onSessionBind func(session.Session)
router *router.Router
rpcClient cluster.RPCClient
rpcServer cluster.RPCServer
metricsReporters []metrics.Reporter
running bool
serializer serialize.Serializer
server *cluster.Server
serverMode ServerMode
serviceDiscovery cluster.ServiceDiscovery
startAt time.Time
worker *worker.Worker
remoteService *service.RemoteService
handlerService *service.HandlerService
handlerComp []regComp
remoteComp []regComp
modulesMap map[string]interfaces.Module
modulesArr []moduleWrapper
groups groups.GroupService
sessionPool session.SessionPool
}
// NewApp is the base constructor for a pitaya app instance
func NewApp(
serverMode ServerMode,
serializer serialize.Serializer,
acceptors []acceptor.Acceptor,
dieChan chan bool,
router *router.Router,
server *cluster.Server,
rpcClient cluster.RPCClient,
rpcServer cluster.RPCServer,
worker *worker.Worker,
serviceDiscovery cluster.ServiceDiscovery,
remoteService *service.RemoteService,
handlerService *service.HandlerService,
groups groups.GroupService,
sessionPool session.SessionPool,
metricsReporters []metrics.Reporter,
config config.PitayaConfig,
) *App {
app := &App{
server: server,
config: config,
rpcClient: rpcClient,
rpcServer: rpcServer,
worker: worker,
serviceDiscovery: serviceDiscovery,
remoteService: remoteService,
handlerService: handlerService,
groups: groups,
debug: false,
startAt: time.Now(),
dieChan: dieChan,
acceptors: acceptors,
metricsReporters: metricsReporters,
serverMode: serverMode,
running: false,
serializer: serializer,
router: router,
handlerComp: make([]regComp, 0),
remoteComp: make([]regComp, 0),
modulesMap: make(map[string]interfaces.Module),
modulesArr: []moduleWrapper{},
sessionPool: sessionPool,
}
if app.heartbeat == time.Duration(0) {
app.heartbeat = config.Heartbeat.Interval
}
app.initSysRemotes()
return app
}
func (app *App) GetRemoteService() *service.RemoteService {
return app.remoteService
}
// GetDieChan gets the channel that the app sinalizes when its going to die
func (app *App) GetDieChan() chan bool {
return app.dieChan
}
// SetDebug toggles debug on/off
func (app *App) SetDebug(debug bool) {
app.debug = debug
}
// SetHeartbeatTime sets the heartbeat time
func (app *App) SetHeartbeatTime(interval time.Duration) {
app.heartbeat = interval
}
// GetServerID returns the generated server id
func (app *App) GetServerID() string {
return app.server.ID
}
// GetMetricsReporters gets registered metrics reporters
func (app *App) GetMetricsReporters() []metrics.Reporter {
return app.metricsReporters
}
// GetServer gets the local server instance
func (app *App) GetServer() *cluster.Server {
return app.server
}
// GetServerByID returns the server with the specified id
func (app *App) GetServerByID(id string) (*cluster.Server, error) {
return app.serviceDiscovery.GetServer(id)
}
// GetServersByType get all servers of type
func (app *App) GetServersByType(t string) (map[string]*cluster.Server, error) {
return app.serviceDiscovery.GetServersByType(t)
}
// GetServers get all servers
func (app *App) GetServers() []*cluster.Server {
return app.serviceDiscovery.GetServers()
}
// IsRunning indicates if the Pitaya app has been initialized. Note: This
// doesn't cover acceptors, only the pitaya internal registration and modules
// initialization.
func (app *App) IsRunning() bool {
return app.running
}
// SetLogger logger setter
func SetLogger(l logging.Logger) {
logger.Log = l
}
func (app *App) initSysRemotes() {
sys := remote.NewSys(app.sessionPool)
app.RegisterRemote(sys,
component.WithName("sys"),
component.WithNameFunc(strings.ToLower),
)
}
func (app *App) periodicMetrics() {
period := app.config.Metrics.Period
go metrics.ReportSysMetrics(app.metricsReporters, period)
if app.worker.Started() {
go worker.Report(app.metricsReporters, period)
}
}
// Start starts the app
func (app *App) Start() {
if !app.server.Frontend && len(app.acceptors) > 0 {
logger.Log.Fatal("acceptors are not allowed on backend servers")
}
if app.server.Frontend && len(app.acceptors) == 0 {
logger.Log.Fatal("frontend servers should have at least one configured acceptor")
}
if app.serverMode == Cluster {
if reflect.TypeOf(app.rpcClient) == reflect.TypeOf(&cluster.GRPCClient{}) {
app.serviceDiscovery.AddListener(app.rpcClient.(*cluster.GRPCClient))
}
if err := app.RegisterModuleBefore(app.rpcServer, "rpcServer"); err != nil {
logger.Log.Fatal("failed to register rpc server module: %s", err.Error())
}
if err := app.RegisterModuleBefore(app.rpcClient, "rpcClient"); err != nil {
logger.Log.Fatal("failed to register rpc client module: %s", err.Error())
}
// set the service discovery as the last module to be started to ensure
// all modules have been properly initialized before the server starts
// receiving requests from other pitaya servers
if err := app.RegisterModuleAfter(app.serviceDiscovery, "serviceDiscovery"); err != nil {
logger.Log.Fatal("failed to register service discovery module: %s", err.Error())
}
}
app.periodicMetrics()
app.listen()
defer func() {
timer.GlobalTicker.Stop()
app.running = false
}()
sg := make(chan os.Signal)
signal.Notify(sg, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM)
// stop server
select {
case <-app.dieChan:
logger.Log.Warn("the app will shutdown in a few seconds")
case s := <-sg:
logger.Log.Warn("got signal: ", s, ", shutting down...")
close(app.dieChan)
}
logger.Log.Warn("server is stopping...")
app.sessionPool.CloseAll()
app.shutdownModules()
app.shutdownComponents()
}
func (app *App) listen() {
app.startupComponents()
// create global ticker instance, timer precision could be customized
// by SetTimerPrecision
timer.GlobalTicker = time.NewTicker(timer.Precision)
logger.Log.Infof("starting server %s:%s", app.server.Type, app.server.ID)
for i := 0; i < app.config.Concurrency.Handler.Dispatch; i++ {
go app.handlerService.Dispatch(i)
}
for _, acc := range app.acceptors {
a := acc
go func() {
for conn := range a.GetConnChan() {
go app.handlerService.Handle(conn)
}
}()
go func() {
a.ListenAndServe()
}()
logger.Log.Infof("listening with acceptor %s on addr %s", reflect.TypeOf(a), a.GetAddr())
}
if app.serverMode == Cluster && app.server.Frontend && app.config.Session.Unique {
unique := mods.NewUniqueSession(app.server, app.rpcServer, app.rpcClient, app.sessionPool)
app.remoteService.AddRemoteBindingListener(unique)
app.RegisterModule(unique, "uniqueSession")
}
app.startModules()
logger.Log.Info("all modules started!")
app.running = true
}
// SetDictionary sets routes map
func (app *App) SetDictionary(dict map[string]uint16) error {
if app.running {
return constants.ErrChangeDictionaryWhileRunning
}
return message.SetDictionary(dict)
}
// AddRoute adds a routing function to a server type
func (app *App) AddRoute(
serverType string,
routingFunction router.RoutingFunc,
) error {
if app.router != nil {
if app.running {
return constants.ErrChangeRouteWhileRunning
}
app.router.AddRoute(serverType, routingFunction)
} else {
return constants.ErrRouterNotInitialized
}
return nil
}
// Shutdown send a signal to let 'pitaya' shutdown itself.
func (app *App) Shutdown() {
select {
case <-app.dieChan: // prevent closing closed channel
default:
close(app.dieChan)
}
}
// Error creates a new error with a code, message and metadata
func Error(err error, code string, metadata ...map[string]string) *errors.Error {
return errors.NewError(err, code, metadata...)
}
// GetSessionFromCtx retrieves a session from a given context
func (app *App) GetSessionFromCtx(ctx context.Context) session.Session {
sessionVal := ctx.Value(constants.SessionCtxKey)
if sessionVal == nil {
logger.Log.Debug("ctx doesn't contain a session, are you calling GetSessionFromCtx from inside a remote?")
return nil
}
return sessionVal.(session.Session)
}
// GetDefaultLoggerFromCtx returns the default logger from the given context
func GetDefaultLoggerFromCtx(ctx context.Context) logging.Logger {
l := ctx.Value(constants.LoggerCtxKey)
if l == nil {
return logger.Log
}
return l.(logging.Logger)
}
// AddMetricTagsToPropagateCtx adds a key and metric tags that will
// be propagated through RPC calls. Use the same tags that are at
// 'pitaya.metrics.additionalTags' config
func AddMetricTagsToPropagateCtx(
ctx context.Context,
tags map[string]string,
) context.Context {
return pcontext.AddToPropagateCtx(ctx, constants.MetricTagsKey, tags)
}
// AddToPropagateCtx adds a key and value that will be propagated through RPC calls
func AddToPropagateCtx(ctx context.Context, key string, val interface{}) context.Context {
return pcontext.AddToPropagateCtx(ctx, key, val)
}
// GetFromPropagateCtx adds a key and value that came through RPC calls
func GetFromPropagateCtx(ctx context.Context, key string) interface{} {
return pcontext.GetFromPropagateCtx(ctx, key)
}
// ExtractSpan retrieves an opentracing span context from the given context
// The span context can be received directly or via an RPC call
func ExtractSpan(ctx context.Context) (opentracing.SpanContext, error) {
return tracing.ExtractSpan(ctx)
}
// Documentation returns handler and remotes documentacion
func (app *App) Documentation(getPtrNames bool) (map[string]interface{}, error) {
handlerDocs, err := app.handlerService.Docs(getPtrNames)
if err != nil {
return nil, err
}
remoteDocs, err := app.remoteService.Docs(getPtrNames)
if err != nil {
return nil, err
}
return map[string]interface{}{
"handlers": handlerDocs,
"remotes": remoteDocs,
}, nil
}
// AddGRPCInfoToMetadata adds host, external host and
// port into metadata
func AddGRPCInfoToMetadata(
metadata map[string]string,
region string,
host, port string,
externalHost, externalPort string,
) map[string]string {
metadata[constants.GRPCHostKey] = host
metadata[constants.GRPCPortKey] = port
metadata[constants.GRPCExternalHostKey] = externalHost
metadata[constants.GRPCExternalPortKey] = externalPort
metadata[constants.RegionKey] = region
return metadata
}
// Descriptor returns the protobuf message descriptor for a given message name
func Descriptor(protoName string) ([]byte, error) {
return docgenerator.ProtoDescriptors(protoName)
}
// StartWorker configures, starts and returns pitaya worker
func (app *App) StartWorker() {
app.worker.Start()
}
// RegisterRPCJob registers rpc job to execute jobs with retries
func (app *App) RegisterRPCJob(rpcJob worker.RPCJob) error {
err := app.worker.RegisterRPCJob(rpcJob)
return err
}