-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmain.go
More file actions
452 lines (386 loc) · 12 KB
/
main.go
File metadata and controls
452 lines (386 loc) · 12 KB
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
package main
import (
"context"
"fmt"
"net"
"net/http"
"net/http/pprof"
"runtime"
"sync"
"time"
_ "time/tzdata"
"golbat/config"
db2 "golbat/db"
"golbat/decoder"
"golbat/external"
pb "golbat/grpc"
"golbat/stats_collector"
"golbat/webhooks"
"github.com/gin-gonic/gin"
"github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
ginlogrus "github.com/toorop/gin-logrus"
"google.golang.org/grpc"
)
var db *sqlx.DB
var dbDetails db2.DbDetails
var statsCollector stats_collector.StatsCollector
func main() {
var wg sync.WaitGroup
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
wg.Add(1)
go func() {
defer wg.Done()
watchForShutdown(ctx, cancelFn)
}()
cfg, err := config.ReadConfig()
if err != nil {
panic(err)
}
logLevel := log.InfoLevel
if cfg.Logging.Debug == true {
logLevel = log.DebugLevel
}
SetupLogger(
logLevel,
cfg.Logging.SaveLogs,
cfg.Logging.MaxSize,
cfg.Logging.MaxAge,
cfg.Logging.MaxBackups,
cfg.Logging.Compress,
)
log.Infof("Golbat starting")
// Both Sentry & Pyroscope are optional and off by default. Read more:
// https://docs.sentry.io/platforms/go
// https://pyroscope.io/docs/golang
external.InitSentry()
external.InitPyroscope()
webhooksSender, err := webhooks.NewWebhooksSender(cfg)
if err != nil {
log.Fatalf("failed to setup webhooks sender: %s", err)
}
decoder.SetWebhooksSender(webhooksSender)
// Capture connection properties.
mysqlConfig := mysql.Config{
User: cfg.Database.User, //"root", //os.Getenv("DBUSER"),
Passwd: cfg.Database.Password, //"transmit", //os.Getenv("DBPASS"),
Net: "tcp",
Addr: cfg.Database.Addr,
DBName: cfg.Database.Db,
AllowNativePasswords: true,
}
dbConnectionString := mysqlConfig.FormatDSN()
driver := "mysql"
log.Infof("Starting migration")
m, err := migrate.New(
"file://sql",
driver+"://"+dbConnectionString+"&multiStatements=true")
if err != nil {
log.Fatal(err)
return
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
log.Fatal(err)
return
}
log.Infof("Opening database for processing, max pool = %d", cfg.Database.MaxPool)
// Get a database handle.
db, err = sqlx.Open(driver, dbConnectionString)
if err != nil {
log.Fatal(err)
return
}
db.SetConnMaxLifetime(time.Minute * 3) // Recommended by go mysql driver
db.SetMaxOpenConns(cfg.Database.MaxPool)
db.SetMaxIdleConns(10)
db.SetConnMaxIdleTime(time.Minute)
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
return
}
log.Infoln("Connected to database")
decoder.SetKojiUrl(cfg.Koji.Url, cfg.Koji.BearerToken)
//if cfg.LegacyInMemory {
// // Initialise in memory db
// inMemoryDb, err = sqlx.Open("sqlite3", ":memory:")
// if err != nil {
// log.Fatal(err)
// return
// }
//
// inMemoryDb.SetMaxOpenConns(1)
//
// pingErr = inMemoryDb.Ping()
// if pingErr != nil {
// log.Fatal(pingErr)
// return
// }
//
// // Create database
// content, fileErr := ioutil.ReadFile("sql/sqlite/create.sql")
//
// if fileErr != nil {
// log.Fatal(err)
// }
//
// inMemoryDb.MustExec(string(content))
//
// dbDetails = db2.DbDetails{
// PokemonDb: inMemoryDb,
// UsePokemonCache: false,
// GeneralDb: db,
// }
//} else {
dbDetails = db2.DbDetails{
PokemonDb: db,
UsePokemonCache: true,
GeneralDb: db,
}
//}
// Create the web server.
gin.SetMode(gin.ReleaseMode)
r := gin.New()
if cfg.Logging.Debug {
r.Use(ginlogrus.Logger(log.StandardLogger()))
} else {
r.Use(gin.Recovery())
}
// choose the statsCollector we will use.
statsCollector = stats_collector.GetStatsCollector(cfg, r)
// tell the decoder the stats collector to use
decoder.SetStatsCollector(statsCollector)
db2.SetStatsCollector(statsCollector)
// collect live stats when prometheus and liveStats are enabled
if cfg.Prometheus.Enabled && cfg.Prometheus.LiveStats {
go db2.PromLiveStatsUpdater(dbDetails, cfg.Prometheus.LiveStatsSleep)
}
needsOhbem := cfg.Pvp.Enabled
needsMasterfile := needsOhbem || cfg.Weather.ProactiveIVSwitching
if needsMasterfile {
if err := decoder.EnsureMasterFileData(); err != nil {
log.Fatalf("Unable to initialise MasterFile: %v", err)
}
}
if needsOhbem {
decoder.InitialiseOhbem()
}
if cfg.Weather.ProactiveIVSwitching {
decoder.InitProactiveIVSwitchSem()
}
if needsMasterfile {
if err := decoder.WatchMasterFileData(); err != nil {
log.Warnf("MasterFile watcher failed: %v", err)
}
}
decoder.LoadStatsGeofences()
decoder.InitWriteBehindQueue(ctx, dbDetails)
InitDeviceCache()
wg.Add(1)
go func() {
defer cancelFn()
defer wg.Done()
err := webhooksSender.Run(ctx)
if err != nil {
log.Errorf("failed to start webhooks sender: %s", err)
}
}()
log.Infoln("Golbat started")
StartDbUsageStatsLogger(db)
decoder.StartStatsWriter(db)
if cfg.Tuning.ExtendedTimeout {
log.Info("Extended timeout enabled")
}
if cfg.Cleanup.Pokemon && (!cfg.PokemonMemoryOnly || cfg.PreserveInMemoryPokemon) {
StartDatabaseArchiver(db)
}
if cfg.Cleanup.Incidents == true {
StartIncidentExpiry(db)
}
if cfg.Cleanup.Tappables == true {
StartTappableExpiry(db)
}
if cfg.Cleanup.Quests == true {
StartQuestExpiry(dbDetails)
}
if cfg.Cleanup.Stats == true {
StartStatsExpiry(db)
}
// init fort tracker for memory-based fort cleanup
staleThreshold := cfg.Cleanup.FortsStaleThreshold
if staleThreshold <= 0 {
staleThreshold = 3600 // def 1 hour
}
decoder.InitFortTracker(staleThreshold)
// Determine loading strategy
// Preload: warms cache for forts, stations, and recent spawnpoints
// FortInMemory: enables rtree spatial lookups (only loads forts)
fortInMemory := cfg.FortInMemory
if cfg.Preload {
// Full preload: loads forts, stations, spawnpoints into cache
// Registers forts with fort tracker, optionally builds rtree
decoder.Preload(dbDetails, fortInMemory)
} else if fortInMemory {
// Fort in memory only: loads forts into cache with rtree
if err := decoder.PreloadForts(dbDetails, true); err != nil {
log.Errorf("failed to preload forts: %s", err)
}
} else {
// No preload: fort tracker loads its own minimal data
if err := decoder.LoadFortsFromDB(ctx, dbDetails); err != nil {
log.Errorf("failed to load forts into tracker: %s", err)
}
}
// Load preserved pokemon if enabled
if cfg.PreserveInMemoryPokemon && cfg.PokemonMemoryOnly {
decoder.PreloadPreservedPokemon(dbDetails)
}
// Start the GRPC receiver
if cfg.GrpcPort > 0 {
log.Infof("Starting GRPC server on port %d", cfg.GrpcPort)
go func() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GrpcPort))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// Initialize gRPC Prometheus metrics if enabled
var grpcServerOpts []grpc.ServerOption
if cfg.Prometheus.Enabled {
srvMetrics := grpcprom.NewServerMetrics(
grpcprom.WithServerHandlingTimeHistogram(
grpcprom.WithHistogramBuckets(cfg.Prometheus.BucketSize),
),
)
grpcServerOpts = append(grpcServerOpts,
grpc.UnaryInterceptor(srvMetrics.UnaryServerInterceptor()),
grpc.StreamInterceptor(srvMetrics.StreamServerInterceptor()),
)
srvMetrics.InitializeMetrics(grpc.NewServer(grpcServerOpts...))
}
s := grpc.NewServer(grpcServerOpts...)
pb.RegisterRawProtoServer(s, &grpcRawServer{})
pb.RegisterPokemonServer(s, &grpcPokemonServer{})
log.Printf("grpc server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
}
r.POST("/raw", Raw)
r.GET("/health", GetHealth)
apiGroup := r.Group("/api", AuthRequired())
apiGroup.GET("/health", GetHealth)
apiGroup.POST("/clear-quests", ClearQuests)
apiGroup.POST("/quest-status", GetQuestStatus)
apiGroup.POST("/pokestop-positions", GetPokestopPositions)
apiGroup.GET("/pokestop/id/:fort_id", GetPokestop)
apiGroup.GET("/gym/id/:gym_id", GetGym)
apiGroup.POST("/gym/query", GetGyms)
apiGroup.POST("/gym/search", SearchGyms)
apiGroup.POST("/gym/scan", GymScan)
apiGroup.POST("/pokestop/scan", PokestopScan)
apiGroup.POST("/station/query", GetStations)
apiGroup.POST("/station/scan", StationScan)
apiGroup.POST("/fort/scan", FortScan)
apiGroup.POST("/reload-geojson", ReloadGeojson)
apiGroup.GET("/reload-geojson", ReloadGeojson)
apiGroup.GET("/pokemon/id/:pokemon_id", PokemonOne)
apiGroup.GET("/pokemon/available", PokemonAvailable)
apiGroup.POST("/pokemon/scan", PokemonScan)
apiGroup.POST("/pokemon/v2/scan", PokemonScan2)
apiGroup.POST("/pokemon/v3/scan", PokemonScan3)
apiGroup.POST("/pokemon/search", PokemonSearch)
apiGroup.GET("/tappable/id/:tappable_id", GetTappable)
apiGroup.GET("/devices/all", GetDevices)
apiGroup.GET("/fort-tracker/cell/:cell_id", GetFortTrackerCell)
apiGroup.GET("/fort-tracker/forts/:fort_id", GetFortTrackerFort)
apiGroup.GET("/skip-preserve-pokemon", SkipPreservePokemon)
apiGroup.POST("/skip-preserve-pokemon", SkipPreservePokemon)
debugGroup := r.Group("/debug")
if cfg.Tuning.ProfileRoutes {
pprofGroup := debugGroup.Group("/pprof", AuthRequired())
pprofGroup.GET("/cmdline", func(c *gin.Context) {
pprof.Cmdline(c.Writer, c.Request)
})
pprofGroup.GET("/heap", func(c *gin.Context) {
pprof.Index(c.Writer, c.Request)
})
pprofGroup.GET("/block", func(c *gin.Context) {
pprof.Handler("block").ServeHTTP(c.Writer, c.Request)
})
pprofGroup.GET("/mutex", func(c *gin.Context) {
pprof.Index(c.Writer, c.Request)
})
pprofGroup.GET("/trace", func(c *gin.Context) {
pprof.Trace(c.Writer, c.Request)
})
pprofGroup.GET("/profile", func(c *gin.Context) {
pprof.Profile(c.Writer, c.Request)
})
pprofGroup.GET("/symbol", func(c *gin.Context) {
pprof.Symbol(c.Writer, c.Request)
})
pprofGroup.GET("/goroutine", func(c *gin.Context) {
pprof.Handler("goroutine").ServeHTTP(c.Writer, c.Request)
})
if config.Config.Tuning.ProfileContention {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
}
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: r,
}
// Start the server in a goroutine, as it will block until told to shutdown.
wg.Add(1)
go func() {
defer cancelFn()
defer wg.Done()
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Errorf("Failed to listen and start http server: %s", err)
}
}()
// wait for shutdown to be signaled in some way. This can be from a failure
// to start the webhook sender, failure to start the http server, and/or
// watchForShutdown() saying it is time to shutdown. (watchForShutdown() on unix
// waits for a SIGINT or SIGTERM)
<-ctx.Done()
log.Info("Starting shutdown...")
// So now we attempt to shutdown the http server, telling it to wait for open requests to
// finish for 5 seconds before just pulling the plug.
shutdownCtx, shutdownCancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancelFn()
if err := srv.Shutdown(shutdownCtx); err != nil {
if err == context.DeadlineExceeded {
log.Warn("Graceful shutdown timed out, exiting.")
} else {
log.Errorf("Error during http server shutdown: %s", err)
}
}
// wait for other started goroutines to cleanup and exit before we flush the
// webhooks and exit the program.
log.Info("http server is shutdown, waiting for other go routines to exit...")
wg.Wait()
log.Info("go routines have exited, flushing write-behind queue...")
decoder.FlushWriteBehindQueue()
// Preserve in-memory pokemon if enabled and not skipped via API
if cfg.PreserveInMemoryPokemon && cfg.PokemonMemoryOnly {
if decoder.ShouldPreservePokemon() {
log.Info("preserving in-memory pokemon to database...")
decoder.PreservePokemonToDatabase(dbDetails)
} else {
log.Info("skipping pokemon preservation (disabled via API)")
}
}
log.Info("flushing webhooks now...")
webhooksSender.Flush()
log.Info("Golbat exiting!")
}