From 9686dba6b1b301ad580537edf0b887e770ac96eb Mon Sep 17 00:00:00 2001 From: John Behm Date: Thu, 18 Jan 2024 20:38:55 +0100 Subject: [PATCH] handle startup gateway event only once & start more message update routines --- .gitignore | 1 + .vscode/launch.json | 10 ++++++++- bot/bot.go | 52 +++++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 88c2402..20437a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env +*.env twstatus-bot .DS_Store *.db diff --git a/.vscode/launch.json b/.vscode/launch.json index 8b0865c..4508b4f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,12 +5,20 @@ "version": "0.2.0", "configurations": [ { - "name": "Launch Package", + "name": "Launch Dev Environment", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}", "args": ["-c", "${workspaceFolder}/.env"] + }, + { + "name": "Launch Prod Environment", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": ["-c", "${workspaceFolder}/prod.env"] } ] } \ No newline at end of file diff --git a/bot/bot.go b/bot/bot.go index 63a8c63..a183155 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -7,6 +7,7 @@ import ( "log" "runtime" "sort" + "sync" "time" "github.com/diamondburned/arikawa/v3/api" @@ -325,33 +326,38 @@ func New( gateway.IntentGuilds | gateway.IntentGuildMessages | gateway.IntentGuildMessageReactions, ) + var startupOnce sync.Once s.AddHandler(func(*gateway.ReadyEvent) { - me, err := s.Me() - if err != nil { - log.Fatalf("failed to get bot user: %v", err) - } - bot.userID = me.ID + // it's possible that the bot occasionally looses the gateway connection + // calling this heavy weight function on every reconnect is not ideal + startupOnce.Do(func() { + me, err := s.Me() + if err != nil { + log.Fatalf("failed to get bot user: %v", err) + } + bot.userID = me.ID - log.Println("connected to the gateway as", me.Tag()) - src, dst, err := bot.updateServers() - if err != nil { - log.Printf("failed to initialize server list: %v", err) - } else { - log.Printf("initialized server list with %d source and %d target servers", src, dst) - } + log.Println("connected to the gateway as", me.Tag()) + src, dst, err := bot.updateServers() + if err != nil { + log.Printf("failed to initialize server list: %v", err) + } else { + log.Printf("initialized server list with %d source and %d target servers", src, dst) + } - // sync trackings and player notification requests - err = bot.syncDatabaseState(ctx) - if err != nil { - log.Fatalf("failed to synchronize database with discord state: %v", err) - } + // sync trackings and player notification requests + err = bot.syncDatabaseState(ctx) + if err != nil { + log.Fatalf("failed to synchronize database with discord state: %v", err) + } - // start polling - go bot.cacheCleanup() - go bot.serverUpdater(pollingInterval) - for i := 0; i < max(runtime.NumCPU(), 2); i++ { - go bot.messageUpdater(i + 1) - } + // start polling + go bot.cacheCleanup() + go bot.serverUpdater(pollingInterval) + for i := 0; i < max(2*runtime.NumCPU(), 5); i++ { + go bot.messageUpdater(i + 1) + } + }) }) // requires guild message intents