From 3432a74d23517b5085cf9eb4db7d151f8ddcc29f Mon Sep 17 00:00:00 2001 From: Medzik <87065584+MedzikUser@users.noreply.github.com> Date: Wed, 11 Aug 2021 10:35:51 +0000 Subject: [PATCH] feat: Add Cache URLs and Database config from toml file - Added Cache URLs from Database - and Database Name and Collection set from toml config file --- .env.schema | 2 -- backend/cache.go | 20 ++++++++++++++++++++ backend/ping.go | 32 ++++++++++++++++++++++---------- backend/ticker.go | 3 ++- config.schema.toml | 1 + config/toml.go | 20 ++++++++++++++++++++ database/mongo/delete.go | 2 +- 7 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 backend/cache.go diff --git a/.env.schema b/.env.schema index c3f6427..aaf85cb 100644 --- a/.env.schema +++ b/.env.schema @@ -1,4 +1,2 @@ # MongoDB MONGODB_URI=mongodb+srv://... -MONGODB_DB=... -MONGODB_COLLECTION=... diff --git a/backend/cache.go b/backend/cache.go new file mode 100644 index 0000000..4c39662 --- /dev/null +++ b/backend/cache.go @@ -0,0 +1,20 @@ +package backend + +import ( + "gitlab.com/gaming0skar123/go/pingbot/database/mongo" +) + +var cacheURL []string + +func cache() { + cacheURL = cacheURL[:0] + + results, err := mongo.GetAll() + if checkErr(err, "get keys from db") { + return + } + + for _, value := range results { + cacheURL = append(cacheURL, value.URL) + } +} diff --git a/backend/ping.go b/backend/ping.go index d47c537..fe57c21 100644 --- a/backend/ping.go +++ b/backend/ping.go @@ -3,31 +3,43 @@ package backend import ( "context" "net/http" + "sync" "time" "gitlab.com/gaming0skar123/go/pingbot/common" - "gitlab.com/gaming0skar123/go/pingbot/database/mongo" + "gitlab.com/gaming0skar123/go/pingbot/config" ) -var checkErr = common.CheckErr +var ( + checkErr = common.CheckErr + cacheRetry int +) func ping() { - results, err := mongo.GetAll() - if checkErr(err, "get keys from db") { - return + if cacheRetry >= config.Toml.Backend.Cache { + cache() + cacheRetry = 0 + } + cacheRetry++ + + var wg sync.WaitGroup + + for _, url := range cacheURL { + go loop(wg, url) } - for _, value := range results { - go loop(value) - } + wg.Wait() } -func loop(value mongo.URL) { +func loop(wg sync.WaitGroup, url string) { + wg.Add(1) + defer wg.Done() + // Timeout 1 minute ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() - req, err := http.NewRequestWithContext(ctx, "GET", value.URL, nil) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if checkErr(err, "new http request") { Status.Error++ return diff --git a/backend/ticker.go b/backend/ticker.go index 5ad6b6c..89ce4c7 100644 --- a/backend/ticker.go +++ b/backend/ticker.go @@ -7,7 +7,8 @@ import ( ) func Ticker() { - // Ping on Start + // On Start + cache() ping() ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute) diff --git a/config.schema.toml b/config.schema.toml index 7e7a1a0..9be0997 100644 --- a/config.schema.toml +++ b/config.schema.toml @@ -5,6 +5,7 @@ port=8080 # Port to listen [backend] enabled=true # Enable Backend ping=3 # Ping every three minutes +cache=5 # Get urls from db every five url pings [autoupdate] enabled=true # Enable Auto Updater diff --git a/config/toml.go b/config/toml.go index 146ec54..a02bf7c 100644 --- a/config/toml.go +++ b/config/toml.go @@ -15,6 +15,7 @@ type tomlConfig struct { Backend backendConfig AutoUpdate autoUpdateConfig Cluster clusterConfig + MongoDB mongoDBConfig } type httpConfig struct { @@ -25,6 +26,7 @@ type httpConfig struct { type backendConfig struct { Enabled bool Ping time.Duration + Cache int } type autoUpdateConfig struct { @@ -37,6 +39,11 @@ type clusterConfig struct { Node int } +type mongoDBConfig struct { + Database string + Collection string +} + var Toml tomlConfig func init() { @@ -49,9 +56,22 @@ func init() { if !common.CheckErr(err, "download default config") { _, err = toml.DecodeFile("./config.toml", &Toml) common.CheckErr(err, "decode toml config") + os.Exit(1) } } } + + if Toml.Backend.Cache == 0 { + Toml.Backend.Cache = 5 + } + + if Toml.MongoDB.Collection != "" { + Mongo_Collection = Toml.MongoDB.Collection + } + + if Toml.MongoDB.Database != "" { + Mongo_DB = Toml.MongoDB.Database + } } func DownloadFile(filepath string, url string) error { diff --git a/database/mongo/delete.go b/database/mongo/delete.go index eda509e..04a3dc2 100644 --- a/database/mongo/delete.go +++ b/database/mongo/delete.go @@ -13,7 +13,7 @@ func Delete(url string) (*mongo.DeleteResult, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - r, err := Coll.DeleteOne(ctx, bson.M{"_id": url}) + r, err := Coll.DeleteOne(ctx, bson.M{"_id": url}) if err != nil { fmt.Print(err) return nil, err