From 29136115d253e2ccc0d70ba789c212aa47b2310e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 26 Aug 2022 13:48:08 +0200 Subject: [PATCH] v0.1.7: All scheduled tasks are now executed in Go routines with random delay on API heavy tasks --- bot/bot.go | 54 ++++++++++++++++++-------------- bot/sc_handler_sot_userstats.go | 7 +++++ bot/se_handler_checkratcookie.go | 6 ++++ config/config.go | 3 +- crypto/random.go | 15 +++++++++ 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index fd64ee6..bb4b852 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -119,14 +119,12 @@ func (b *Bot) Run() error { signal.Notify(sc) // Timer events - var rn int - rn = int(b.Config.Timer.FHSpam) - rn, err = crypto.RandNum(int(b.Config.Timer.FHSpam)) + rd, err := crypto.RandDuration(b.Config.Timer.FHSpam, "m") if err != nil { ll.Warn().Msgf("failed to generate random number for FH timer: %s", err) - rn = int(b.Config.Timer.FHSpam) + rd = time.Minute * time.Duration(b.Config.Timer.FHSpam) } - fht := time.NewTicker(time.Duration(int64(rn)+b.Config.Timer.FHSpam) * time.Minute) + fht := time.NewTicker(rd) defer fht.Stop() trt := time.NewTicker(b.Config.Timer.TRUpdate) defer trt.Stop() @@ -170,33 +168,43 @@ func (b *Bot) Run() error { return nil } case <-fht.C: - if err := b.ScheduledEventSoTFlameheart(); err != nil { - ll.Error().Msgf("failed to process scheuled flameheart event: %s", err) - } + go func() { + if err := b.ScheduledEventSoTFlameheart(); err != nil { + ll.Error().Msgf("failed to process scheuled flameheart event: %s", err) + } + }() // Reset the duration - rn, err = crypto.RandNum(int(b.Config.Timer.FHSpam)) + rd, err := crypto.RandDuration(b.Config.Timer.FHSpam, "m") if err != nil { ll.Warn().Msgf("failed to generate random number for FH timer: %s", err) - rn = int(b.Config.Timer.FHSpam) + rd = time.Minute * time.Duration(b.Config.Timer.FHSpam) } - fht.Reset(time.Duration(int64(rn)+b.Config.Timer.FHSpam) * time.Minute) + fht.Reset(rd) case <-trt.C: - if err := b.ScheduledEventUpdateTradeRoutes(); err != nil { - ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err) - } + go func() { + if err := b.ScheduledEventUpdateTradeRoutes(); err != nil { + ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err) + } + }() case <-ust.C: - if err := b.ScheduledEventUpdateUserStats(); err != nil { - ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err) - } + go func() { + if err := b.ScheduledEventUpdateUserStats(); err != nil { + ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err) + } + }() case <-rct.C: - if err := b.ScheduledEventCheckRATCookies(); err != nil { - ll.Error().Msgf("failed to process scheuled RAT cookie check event: %s", err) - } + go func() { + if err := b.ScheduledEventCheckRATCookies(); err != nil { + ll.Error().Msgf("failed to process scheuled RAT cookie check event: %s", err) + } + }() case <-ddt.C: - if err := b.ScheduledEventUpdateDailyDeeds(); err != nil { - ll.Error().Msgf("failed to process scheuled daily deeds update event: %s", err) - } + go func() { + if err := b.ScheduledEventUpdateDailyDeeds(); err != nil { + ll.Error().Msgf("failed to process scheuled daily deeds update event: %s", err) + } + }() } } } diff --git a/bot/sc_handler_sot_userstats.go b/bot/sc_handler_sot_userstats.go index 028b918..60f658a 100644 --- a/bot/sc_handler_sot_userstats.go +++ b/bot/sc_handler_sot_userstats.go @@ -5,10 +5,12 @@ import ( "errors" "fmt" "github.com/bwmarrin/discordgo" + "github.com/wneessen/arrgo/crypto" "github.com/wneessen/arrgo/model" "golang.org/x/text/language" "golang.org/x/text/message" "net/http" + "time" ) // SoTUserOverview represents the JSON structure of the Sea of Thieves user overview API response @@ -226,6 +228,11 @@ func (b *Bot) ScheduledEventUpdateUserStats() error { ll.Error().Msgf("failed to store user stats in DB: %s", err) continue } + rd, err := crypto.RandDuration(10, "s") + if err != nil { + rd = time.Second * 10 + } + time.Sleep(rd) } return nil } diff --git a/bot/se_handler_checkratcookie.go b/bot/se_handler_checkratcookie.go index b955c02..df9e692 100644 --- a/bot/se_handler_checkratcookie.go +++ b/bot/se_handler_checkratcookie.go @@ -3,6 +3,7 @@ package bot import ( "errors" "fmt" + "github.com/wneessen/arrgo/crypto" "github.com/wneessen/arrgo/model" "net/http" "time" @@ -89,6 +90,11 @@ func (b *Bot) ScheduledEventCheckRATCookies() error { } } } + rd, err := crypto.RandDuration(10, "s") + if err != nil { + rd = time.Second * 10 + } + time.Sleep(rd) } return nil } diff --git a/config/config.go b/config/config.go index d842b41..643502b 100644 --- a/config/config.go +++ b/config/config.go @@ -44,11 +44,12 @@ type Config struct { EncryptionKey string `fig:"enc_key"` } Timer struct { - FHSpam int64 `fig:"flameheart_spam" default:"60"` + FHSpam int `fig:"flameheart_spam" default:"60"` TRUpdate time.Duration `fig:"traderoutes_update" default:"12h"` USUpdate time.Duration `fig:"userstats_update" default:"6h"` RCCheck time.Duration `fig:"ratcookie_check" default:"6h"` DDUpdate time.Duration `fig:"dailydeed_update" default:"24h"` + ULUpdate time.Duration `fig:"userledger_update" default:"6h"` } confPath string confFile string diff --git a/crypto/random.go b/crypto/random.go index f539ab7..957844a 100644 --- a/crypto/random.go +++ b/crypto/random.go @@ -6,6 +6,7 @@ import ( "fmt" "math/big" "strings" + "time" ) // Range of characters for the different types of string generations @@ -89,6 +90,20 @@ func RandNum(n int) (int, error) { return rn, nil } +// RandDuration returns a random time.Duration in u with a maximum value of the given n +func RandDuration(n int, u string) (time.Duration, error) { + var d time.Duration + rn, err := RandNum(n) + if err != nil { + return d, err + } + d, err = time.ParseDuration(fmt.Sprintf("%d%s", rn, u)) + if err != nil { + return d, err + } + return d, nil +} + // getCharRange returns the range of characters as controlled by the s and h bools func getCharRange(s, h bool) string { var cr string