optimizations for storage calculations

merge-requests/4/head
Matthew 2020-06-28 22:11:17 -04:00
parent f8d466686d
commit 81b19b6267
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
1 changed files with 21 additions and 19 deletions

View File

@ -13,8 +13,13 @@ import (
"time" "time"
) )
// Collection the MongoDB Account collection // ConfigStruct the configuration struct
var Collection *mongo.Collection type ConfigStruct struct {
MongoDB string `json:"mongoURL"`
}
// Config config
var Config *ConfigStruct
// RedisClient the Redis client // RedisClient the Redis client
var RedisClient *redis.Client var RedisClient *redis.Client
@ -40,24 +45,10 @@ func HandleError(e error, serv int) {
func main() { func main() {
var status bool var status bool
type Config struct { Config := &ConfigStruct{}
MongoDB string `json:"mongoURL"`
}
config := &Config{}
file, err := ioutil.ReadFile("../config.json") file, err := ioutil.ReadFile("../config.json")
HandleError(err, 1) HandleError(err, 1)
err = json.Unmarshal(file, &config) 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")
RedisClient = redis.NewClient(&redis.Options{ RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
@ -81,7 +72,16 @@ func main() {
func handler(status* bool) { func handler(status* bool) {
*status = true *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) HandleError(err, 0)
for cur.Next(context.TODO()) { 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()) fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String())
time.Sleep(600000 * time.Millisecond) time.Sleep(600000 * time.Millisecond)
} }
err = mongoClient.Disconnect(ctx)
HandleError(err, 1)
*status = false *status = false
} }