Skip to content

Commit 70b89a4

Browse files
authored
use unique contexts (#41)
1 parent c5c1afc commit 70b89a4

File tree

2 files changed

+6
-11
lines changed
  • source/includes

2 files changed

+6
-11
lines changed

source/includes/fundamentals/code-snippets/srv.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"time"
76

87
"go.mongodb.org/mongo-driver/mongo"
98
"go.mongodb.org/mongo-driver/mongo/options"
@@ -14,23 +13,20 @@ import (
1413
const uri = "mongodb://user:[email protected]:27017/?maxPoolSize=20&w=majority"
1514

1615
func main() {
17-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
18-
defer cancel()
19-
2016
// Create a new client and connect to the server
21-
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
17+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
2218

2319
if err != nil {
2420
panic(err)
2521
}
2622
defer func() {
27-
if err = client.Disconnect(ctx); err != nil {
23+
if err = client.Disconnect(context.TODO()); err != nil {
2824
panic(err)
2925
}
3026
}()
3127

3228
// Ping the primary
33-
if err := client.Ping(ctx, readpref.Primary()); err != nil {
29+
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
3430
panic(err)
3531
}
3632

source/includes/quick-start/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ import (
1313
)
1414

1515
func main() {
16-
ctx := context.TODO()
1716
uri := os.Getenv("MONGODB_URI")
1817
if uri == "" {
1918
log.Panic(`'uri' is empty. Ensure you set the 'MONGODB_URI'
2019
environment variable, or set the value of 'uri' to your
2120
Atlas connection string.`)
2221
}
23-
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
22+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
2423
if err != nil {
2524
log.Panic(err)
2625
}
2726

2827
defer func() {
29-
if err := client.Disconnect(ctx); err != nil {
28+
if err := client.Disconnect(context.TODO()); err != nil {
3029
log.Panic(err)
3130
}
3231
}()
@@ -35,7 +34,7 @@ func main() {
3534
title := "Back to the Future"
3635

3736
var result bson.M
38-
err = coll.FindOne(ctx, bson.D{{"title", title}}).Decode(&result)
37+
err = coll.FindOne(context.TODO(), bson.D{{"title", title}}).Decode(&result)
3938
if err == mongo.ErrNoDocuments {
4039
fmt.Printf("No document was found with the title %s\n", title)
4140
return

0 commit comments

Comments
 (0)