use cancel to not leak memory

This commit is contained in:
lyx0 2021-10-28 15:22:19 +02:00
parent df2aad908f
commit 20f9470ce8
3 changed files with 10 additions and 3 deletions

View file

@ -13,7 +13,9 @@ func AddChannel(channelName string) {
client := Connect()
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
/*
Get my collection instance
*/

View file

@ -19,7 +19,10 @@ func Connect() *mongo.Client {
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)

View file

@ -12,7 +12,9 @@ func InitialJoin(nb *bot.Bot) {
client := Connect()
collection := client.Database("nourybot").Collection("channels")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
defer client.Disconnect(ctx)
cur, currErr := collection.Find(ctx, bson.D{})