diff --git a/src/highlanderticketing/db/db.go b/src/highlanderticketing/db/db.go index 625b4251d79dcdbe8d33abf7574213bae730696d..be259014283a8e3a94c5b117956da9415eb60816 100644 --- a/src/highlanderticketing/db/db.go +++ b/src/highlanderticketing/db/db.go @@ -8,43 +8,34 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -/* - Used to create a singleton object of MongoDB client. - -Initialized and exposed through GetMongoClient(). -*/ -var clientInstance *mongo.Client - -// Used during creation of singleton client object in GetMongoClient(). -var clientInstanceError error - -// Used to execute client creation procedure only once. -var mongoOnce sync.Once +var ( + clientInstance *mongo.Client + clientInstanceError error + clientOnce sync.Once +) -// I have used below constants just to hold required database config's. const ( CONNECTIONSTRING = "mongodb://localhost:27017" DB = "db_issue_manager" - ISSUES = "col_issues" + MATCHES = "col_matches" + POOL_SIZE = 10 // Anzahl der Verbindungen im Pool ) -// GetMongoClient - Return mongodb connection to work with func GetMongoClient() (*mongo.Client, error) { - //Perform connection creation operation only once. - mongoOnce.Do(func() { - // Set client options + clientOnce.Do(func() { + // Erstelle den Verbindungspool clientOptions := options.Client().ApplyURI(CONNECTIONSTRING) - // Connect to MongoDB + clientOptions.SetMaxPoolSize(POOL_SIZE) client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { clientInstanceError = err } - // Check the connection err = client.Ping(context.TODO(), nil) if err != nil { clientInstanceError = err } clientInstance = client }) + return clientInstance, clientInstanceError } diff --git a/src/highlanderticketing/service/match.go b/src/highlanderticketing/service/match.go index adfb0e73f65e8691a98e7147730884aa13028002..356bcb6416341ba16f69367879a3b08becfa93d7 100644 --- a/src/highlanderticketing/service/match.go +++ b/src/highlanderticketing/service/match.go @@ -14,7 +14,7 @@ func CreateMatch(match *model.Match) error { if err != nil { return err } - collection := client.Database(db.DB).Collection(db.ISSUES) + collection := client.Database(db.DB).Collection(db.MATCHES) _, err = collection.InsertOne(context.TODO(), match) if err != nil { @@ -32,7 +32,7 @@ func GetAllMatches() ([]model.Match, error) { return matches, err } - collection := client.Database(db.DB).Collection(db.ISSUES) + collection := client.Database(db.DB).Collection(db.MATCHES) cur, err := collection.Find(context.TODO(), filter) if err != nil { return matches, err