forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
617 lines (534 loc) · 17.6 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// 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.
package pitaya
import (
"context"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"time"
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/spf13/viper"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/conn/codec"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
"github.com/topfreegames/pitaya/defaultpipelines"
"github.com/topfreegames/pitaya/docgenerator"
"github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/metrics"
mods "github.com/topfreegames/pitaya/modules"
"github.com/topfreegames/pitaya/remote"
"github.com/topfreegames/pitaya/router"
"github.com/topfreegames/pitaya/serialize"
"github.com/topfreegames/pitaya/serialize/json"
"github.com/topfreegames/pitaya/service"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/timer"
"github.com/topfreegames/pitaya/tracing"
"github.com/topfreegames/pitaya/worker"
)
// 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
)
// App is the base app struct
type App struct {
acceptors []acceptor.Acceptor
config *config.Config
configured bool
debug bool
dieChan chan bool
heartbeat time.Duration
onSessionBind func(*session.Session)
messageEncoder message.Encoder
packetDecoder codec.PacketDecoder
packetEncoder codec.PacketEncoder
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
}
var (
app = &App{
server: cluster.NewServer(uuid.New().String(), "game", true, map[string]string{}),
debug: false,
startAt: time.Now(),
dieChan: make(chan bool),
acceptors: []acceptor.Acceptor{},
packetDecoder: codec.NewPomeloPacketDecoder(),
packetEncoder: codec.NewPomeloPacketEncoder(),
metricsReporters: make([]metrics.Reporter, 0),
serverMode: Standalone,
serializer: json.NewSerializer(),
configured: false,
running: false,
router: router.New(),
}
remoteService *service.RemoteService
handlerService *service.HandlerService
)
// Configure configures the app
func Configure(
isFrontend bool,
serverType string,
serverMode ServerMode,
serverMetadata map[string]string,
cfgs ...*viper.Viper,
) {
if app.configured {
logger.Log.Warn("pitaya configured twice!")
}
app.config = config.NewConfig(cfgs...)
if app.heartbeat == time.Duration(0) {
app.heartbeat = app.config.GetDuration("pitaya.heartbeat.interval")
}
app.server.Frontend = isFrontend
app.server.Type = serverType
app.serverMode = serverMode
app.server.Metadata = serverMetadata
app.messageEncoder = message.NewMessagesEncoder(app.config.GetBool("pitaya.handler.messages.compression"))
configureMetrics(serverType)
configureDefaultPipelines(app.config)
app.configured = true
}
func configureMetrics(serverType string) {
app.metricsReporters = make([]metrics.Reporter, 0)
constTags := app.config.GetStringMapString("pitaya.metrics.constTags")
if app.config.GetBool("pitaya.metrics.prometheus.enabled") {
port := app.config.GetInt("pitaya.metrics.prometheus.port")
logger.Log.Infof("prometheus is enabled, configuring reporter on port %d", port)
prometheus, err := metrics.GetPrometheusReporter(serverType, app.config, constTags)
if err != nil {
logger.Log.Errorf("failed to start prometheus metrics reporter, skipping %v", err)
} else {
AddMetricsReporter(prometheus)
}
} else {
logger.Log.Info("prometheus is disabled, reporter will not be enabled")
}
if app.config.GetBool("pitaya.metrics.statsd.enabled") {
logger.Log.Infof(
"statsd is enabled, configuring the metrics reporter with host: %s",
app.config.Get("pitaya.metrics.statsd.host"),
)
metricsReporter, err := metrics.NewStatsdReporter(
app.config,
serverType,
constTags,
)
if err != nil {
logger.Log.Errorf("failed to start statds metrics reporter, skipping %v", err)
} else {
logger.Log.Info("successfully configured statsd metrics reporter")
AddMetricsReporter(metricsReporter)
}
}
}
func configureDefaultPipelines(config *config.Config) {
if config.GetBool("pitaya.defaultpipelines.structvalidation.enabled") {
BeforeHandler(defaultpipelines.StructValidatorInstance.Validate)
}
}
// AddAcceptor adds a new acceptor to app
func AddAcceptor(ac acceptor.Acceptor) {
if !app.server.Frontend {
logger.Log.Error("tried to add an acceptor to a backend server, skipping")
return
}
app.acceptors = append(app.acceptors, ac)
}
// GetDieChan gets the channel that the app sinalizes when its going to die
func GetDieChan() chan bool {
return app.dieChan
}
// SetDebug toggles debug on/off
func SetDebug(debug bool) {
app.debug = debug
}
// SetPacketDecoder changes the decoder used to parse messages received
func SetPacketDecoder(d codec.PacketDecoder) {
app.packetDecoder = d
}
// SetPacketEncoder changes the encoder used to package outgoing messages
func SetPacketEncoder(e codec.PacketEncoder) {
app.packetEncoder = e
}
// SetHeartbeatTime sets the heartbeat time
func SetHeartbeatTime(interval time.Duration) {
app.heartbeat = interval
}
// SetLogger logger setter
func SetLogger(l logger.Logger) {
logger.Log = l
}
// GetServerID returns the generated server id
func GetServerID() string {
return app.server.ID
}
// GetConfig gets the pitaya config instance
func GetConfig() *config.Config {
return app.config
}
// GetMetricsReporters gets registered metrics reporters
func GetMetricsReporters() []metrics.Reporter {
return app.metricsReporters
}
// SetRPCServer to be used
func SetRPCServer(s cluster.RPCServer) {
app.rpcServer = s
}
// SetRPCClient to be used
func SetRPCClient(s cluster.RPCClient) {
app.rpcClient = s
}
// SetServiceDiscoveryClient to be used
func SetServiceDiscoveryClient(s cluster.ServiceDiscovery) {
app.serviceDiscovery = s
}
// SetSerializer customize application serializer, which automatically Marshal
// and UnMarshal handler payload
func SetSerializer(seri serialize.Serializer) {
app.serializer = seri
}
// GetSerializer gets the app serializer
func GetSerializer() serialize.Serializer {
return app.serializer
}
// GetServer gets the local server instance
func GetServer() *cluster.Server {
return app.server
}
// GetServerByID returns the server with the specified id
func GetServerByID(id string) (*cluster.Server, error) {
return app.serviceDiscovery.GetServer(id)
}
// GetServersByType get all servers of type
func GetServersByType(t string) (map[string]*cluster.Server, error) {
return app.serviceDiscovery.GetServersByType(t)
}
// GetServers get all servers
func GetServers() []*cluster.Server {
return app.serviceDiscovery.GetServers()
}
// AddMetricsReporter to be used
func AddMetricsReporter(mr metrics.Reporter) {
app.metricsReporters = append(app.metricsReporters, mr)
}
func startDefaultSD() {
// initialize default service discovery
var err error
app.serviceDiscovery, err = cluster.NewEtcdServiceDiscovery(
app.config,
app.server,
app.dieChan,
)
if err != nil {
logger.Log.Fatalf("error starting cluster service discovery component: %s", err.Error())
}
}
func startDefaultRPCServer() {
// initialize default rpc server
rpcServer, err := cluster.NewNatsRPCServer(app.config, app.server, app.metricsReporters, app.dieChan)
if err != nil {
logger.Log.Fatalf("error starting cluster rpc server component: %s", err.Error())
}
SetRPCServer(rpcServer)
}
func startDefaultRPCClient() {
// initialize default rpc client
rpcClient, err := cluster.NewNatsRPCClient(app.config, app.server, app.metricsReporters, app.dieChan)
if err != nil {
logger.Log.Fatalf("error starting cluster rpc client component: %s", err.Error())
}
SetRPCClient(rpcClient)
}
func initSysRemotes() {
sys := &remote.Sys{}
RegisterRemote(sys,
component.WithName("sys"),
component.WithNameFunc(strings.ToLower),
)
}
func periodicMetrics() {
period := app.config.GetDuration("pitaya.metrics.periodicMetrics.period")
go metrics.ReportSysMetrics(app.metricsReporters, period)
if app.worker.Started() {
go worker.Report(app.metricsReporters, period)
}
}
// Start starts the app
func Start() {
if !app.configured {
logger.Log.Fatal("starting app without configuring it first! call pitaya.Configure()")
}
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 app.serviceDiscovery == nil {
logger.Log.Warn("creating default service discovery because cluster mode is enabled, " +
"if you want to specify yours, use pitaya.SetServiceDiscoveryClient")
startDefaultSD()
}
if app.rpcServer == nil {
logger.Log.Warn("creating default rpc server because cluster mode is enabled, " +
"if you want to specify yours, use pitaya.SetRPCServer")
startDefaultRPCServer()
}
if app.rpcClient == nil {
logger.Log.Warn("creating default rpc client because cluster mode is enabled, " +
"if you want to specify yours, use pitaya.SetRPCClient")
startDefaultRPCClient()
}
if reflect.TypeOf(app.rpcClient) == reflect.TypeOf(&cluster.GRPCClient{}) {
app.serviceDiscovery.AddListener(app.rpcClient.(*cluster.GRPCClient))
}
err := RegisterModuleBefore(app.serviceDiscovery, "serviceDiscovery")
if err != nil {
logger.Log.Fatal("failed to register service discovery module: %s", err.Error())
}
err = RegisterModuleBefore(app.rpcServer, "rpcServer")
if err != nil {
logger.Log.Fatal("failed to register rpc server module: %s", err.Error())
}
err = RegisterModuleBefore(app.rpcClient, "rpcClient")
if err != nil {
logger.Log.Fatal("failed to register rpc client module: %s", err.Error())
}
app.router.SetServiceDiscovery(app.serviceDiscovery)
remoteService = service.NewRemoteService(
app.rpcClient,
app.rpcServer,
app.serviceDiscovery,
app.packetEncoder,
app.serializer,
app.router,
app.messageEncoder,
app.server,
)
app.rpcServer.SetPitayaServer(remoteService)
initSysRemotes()
}
handlerService = service.NewHandlerService(
app.dieChan,
app.packetDecoder,
app.packetEncoder,
app.serializer,
app.heartbeat,
app.config.GetInt("pitaya.buffer.agent.messages"),
app.config.GetInt("pitaya.buffer.handler.localprocess"),
app.config.GetInt("pitaya.buffer.handler.remoteprocess"),
app.server,
remoteService,
app.messageEncoder,
app.metricsReporters,
)
periodicMetrics()
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...")
shutdownModules()
shutdownComponents()
}
func listen() {
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.GetInt("pitaya.concurrency.handler.dispatch"); i++ {
go handlerService.Dispatch(i)
}
for _, acc := range app.acceptors {
a := acc
go func() {
for conn := range a.GetConnChan() {
go 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.GetBool("pitaya.session.unique") {
unique := mods.NewUniqueSession(app.server, app.rpcServer, app.rpcClient)
remoteService.AddRemoteBindingListener(unique)
RegisterModule(unique, "uniqueSession")
}
startModules()
logger.Log.Info("all modules started!")
app.running = true
}
// SetDictionary sets routes map
func 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 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 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 GetSessionFromCtx(ctx context.Context) *session.Session {
sessionVal := ctx.Value(constants.SessionCtxKey)
if sessionVal == nil {
logger.Log.Warn("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) logger.Logger {
return ctx.Value(constants.LoggerCtxKey).(logger.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 Documentation(getPtrNames bool) (map[string]interface{}, error) {
handlerDocs, err := handlerService.Docs(getPtrNames)
if err != nil {
return nil, err
}
remoteDocs, err := 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 StartWorker(config *config.Config) error {
var err error
app.worker, err = worker.NewWorker(config)
if err != nil {
return err
}
app.worker.Start()
return nil
}
// RegisterRPCJob registers rpc job to execute jobs with retries
func RegisterRPCJob(rpcJob worker.RPCJob) error {
err := app.worker.RegisterRPCJob(rpcJob)
return err
}