From 81b19b626734732023f4668e7d8ec1f3970f007c Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 28 Jun 2020 22:11:17 -0400 Subject: [PATCH] optimizations for storage calculations --- src/go/storage/storage.go | 40 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/go/storage/storage.go b/src/go/storage/storage.go index 5ac393d..eb60035 100644 --- a/src/go/storage/storage.go +++ b/src/go/storage/storage.go @@ -13,8 +13,13 @@ import ( "time" ) -// Collection the MongoDB Account collection -var Collection *mongo.Collection +// ConfigStruct the configuration struct +type ConfigStruct struct { + MongoDB string `json:"mongoURL"` +} + +// Config config +var Config *ConfigStruct // RedisClient the Redis client var RedisClient *redis.Client @@ -40,24 +45,10 @@ func HandleError(e error, serv int) { func main() { var status bool - type Config struct { - MongoDB string `json:"mongoURL"` - } - config := &Config{} + Config := &ConfigStruct{} file, err := ioutil.ReadFile("../config.json") HandleError(err, 1) - err = json.Unmarshal(file, &config) - - client, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB)) - HandleError(err, 1) - ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) - err = client.Connect(ctx) - HandleError(err, 1) - err = client.Ping(context.TODO(), nil) - fmt.Printf("Connected to MongoDB [GO]\n") - HandleError(err, 1) - - Collection = client.Database("cloudservices").Collection("accounts") + err = json.Unmarshal(file, &Config) RedisClient = redis.NewClient(&redis.Options{ Addr: "localhost:6379", @@ -81,7 +72,16 @@ func main() { func handler(status* bool) { *status = true - cur, err := Collection.Find(context.TODO(), bson.D{}) + mongoClient, err := mongo.NewClient(options.Client().ApplyURI(Config.MongoDB)) + HandleError(err, 1) + ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) + err = mongoClient.Connect(ctx) + HandleError(err, 1) + err = mongoClient.Ping(context.TODO(), nil) + fmt.Printf("Connected to MongoDB [GO]\n") + HandleError(err, 1) + collection := mongoClient.Database("cloudservices").Collection("accounts") + cur, err := collection.Find(context.TODO(), bson.D{}) HandleError(err, 0) for cur.Next(context.TODO()) { @@ -89,6 +89,8 @@ func handler(status* bool) { fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String()) time.Sleep(600000 * time.Millisecond) } + err = mongoClient.Disconnect(ctx) + HandleError(err, 1) *status = false }