Skip to content

Commit

Permalink
GODRIVER-979 Remove context parameter from Connect (mongodb#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored Dec 6, 2023
1 parent ec2153b commit 5b711e8
Show file tree
Hide file tree
Showing 47 changed files with 115 additions and 135 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import (

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))
```

Make sure to defer a call to `Disconnect` after instantiating your client:
Expand Down Expand Up @@ -166,12 +166,12 @@ Compression can be enabled using the `compressors` parameter on the connection s

```go
opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

```go
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

If compressors are set, the Go Driver negotiates with the server to select the first common compressor. For server configuration and defaults, refer to [`networkMessageCompressors`](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--networkMessageCompressors).
Expand Down
4 changes: 2 additions & 2 deletions benchmark/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions benchmark/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BenchmarkClientWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func BenchmarkClientBulkWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func BenchmarkClientRead(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const (
largeData = "large_doc.json"
)

func getClientDB(ctx context.Context) (*mongo.Database, error) {
func getClientDB() (*mongo.Database, error) {
cs, err := integtest.GetConnString()
if err != nil {
return nil, err
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cs.String()))
client, err := mongo.Connect(options.Client().ApplyURI(cs.String()))
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/testatlas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
}

func runTest(ctx context.Context, clientOpts *options.ClientOptions) error {
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
return fmt.Errorf("Connect error: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/testaws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
uri := os.Getenv("MONGODB_URI")
ctx := context.Background()

client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
panic(fmt.Sprintf("Connect error: %v", err))
}
Expand Down
1 change: 0 additions & 1 deletion cmd/testentauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func main() {
compressor := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR")

client, err := mongo.Connect(
context.Background(),
options.Client().ApplyURI(uri).SetCompressors([]string{compressor}))
if err != nil {
log.Fatalf("Error connecting client: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/testkms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {
}

cOpts := options.Client().ApplyURI(uri)
keyVaultClient, err := mongo.Connect(context.Background(), cOpts)
keyVaultClient, err := mongo.Connect(cOpts)
if err != nil {
panic(fmt.Sprintf("Connect error: %v", err))
}
Expand Down
6 changes: 3 additions & 3 deletions event/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// },
// }
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(cmdMonitor)
// client, err := mongo.Connect(context.Background(), clientOpts)
// client, err := mongo.Connect( clientOpts)
//
// Monitoring the connection pool requires specifying a PoolMonitor when constructing
// a mongo.Client. The following code tracks the number of checked out connections:
Expand All @@ -38,7 +38,7 @@
// },
// }
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetPoolMonitor(poolMonitor)
// client, err := mongo.Connect(context.Background(), clientOpts)
// client, err := mongo.Connect( clientOpts)
//
// Monitoring server changes specifying a ServerMonitor object when constructing
// a mongo.Client. Different functions can be set on the ServerMonitor to
Expand All @@ -52,5 +52,5 @@
// }
// }
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetServerMonitor(svrMonitor)
// client, err := mongo.Connect(context.Background(), clientOpts)
// client, err := mongo.Connect( clientOpts)
package event
2 changes: 1 addition & 1 deletion event/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ExampleCommandMonitor() {
},
}
clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(cmdMonitor)
client, err := mongo.Connect(context.Background(), clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/_logger/logrus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
ApplyURI("mongodb://localhost:27017").
SetLoggerOptions(loggerOptions)

client, err := mongo.Connect(context.TODO(), clientOptions)
client, err := mongo.Connect(clientOptions)
if err != nil {
log.Fatalf("error connecting to MongoDB: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/_logger/zap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
ApplyURI("mongodb://localhost:27017").
SetLoggerOptions(loggerOptions)

client, err := mongo.Connect(context.TODO(), clientOptions)
client, err := mongo.Connect(clientOptions)
if err != nil {
log.Fatalf("error connecting to MongoDB: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/_logger/zerolog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
ApplyURI("mongodb://localhost:27017").
SetLoggerOptions(loggerOptions)

client, err := mongo.Connect(context.TODO(), clientOptions)
client, err := mongo.Connect(clientOptions)
if err != nil {
log.Fatalf("error connecting to MongoDB: %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions examples/documentation_examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ func WithTransactionExample(ctx context.Context) error {
uri := mtest.ClusterURI()

clientOpts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
return err
}
Expand Down Expand Up @@ -2781,7 +2781,7 @@ func StableAPIExample() {

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
panic(err)
}
Expand All @@ -2803,7 +2803,7 @@ func StableAPIStrictExample() {

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(true)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
panic(err)
}
Expand All @@ -2825,7 +2825,7 @@ func StableAPINonStrictExample() {

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(false)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
panic(err)
}
Expand All @@ -2848,7 +2848,7 @@ func StableAPIDeprecationErrorsExample() {

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetDeprecationErrors(true)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
client, err := mongo.Connect(clientOpts)
if err != nil {
panic(err)
}
Expand All @@ -2869,7 +2869,7 @@ func StableAPIStrictCountExample(t *testing.T) {
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(true)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)

client, err := mongo.Connect(context.TODO(), clientOpts)
client, err := mongo.Connect(clientOpts)
assert.Nil(t, err, "Connect error: %v", err)
defer func() { _ = client.Disconnect(context.TODO()) }()

Expand Down
2 changes: 1 addition & 1 deletion examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestDocumentationExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mtest.ClusterURI()))
client, err := mongo.Connect(options.Client().ApplyURI(mtest.ClusterURI()))
assert.NoError(t, err)
defer client.Disconnect(ctx)

Expand Down
3 changes: 1 addition & 2 deletions internal/test/compilecheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package main

import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -16,6 +15,6 @@ import (
)

func main() {
_, _ = mongo.Connect(context.Background(), options.Client())
_, _ = mongo.Connect(options.Client())
fmt.Println(bson.D{{Key: "key", Value: "value"}})
}
14 changes: 7 additions & 7 deletions mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ type Client struct {
//
// The Client.Ping method can be used to verify that the deployment is successfully connected and the
// Client was correctly configured.
func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, error) {
func Connect(opts ...*options.ClientOptions) (*Client, error) {
c, err := newClient(opts...)
if err != nil {
return nil, err
}
err = c.connect(ctx)
err = c.connect()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func newClient(opts ...*options.ClientOptions) (*Client, error) {
//
// Connect starts background goroutines to monitor the state of the deployment and does not do any I/O in the main
// goroutine. The Client.Ping method can be used to verify that the connection was created successfully.
func (c *Client) connect(ctx context.Context) error {
func (c *Client) connect() error {
if connector, ok := c.deployment.(driver.Connector); ok {
err := connector.Connect()
if err != nil {
Expand All @@ -246,25 +246,25 @@ func (c *Client) connect(ctx context.Context) error {
}

if c.mongocryptdFLE != nil {
if err := c.mongocryptdFLE.connect(ctx); err != nil {
if err := c.mongocryptdFLE.connect(); err != nil {
return err
}
}

if c.internalClientFLE != nil {
if err := c.internalClientFLE.connect(ctx); err != nil {
if err := c.internalClientFLE.connect(); err != nil {
return err
}
}

if c.keyVaultClientFLE != nil && c.keyVaultClientFLE != c.internalClientFLE && c.keyVaultClientFLE != c {
if err := c.keyVaultClientFLE.connect(ctx); err != nil {
if err := c.keyVaultClientFLE.connect(); err != nil {
return err
}
}

if c.metadataClientFLE != nil && c.metadataClientFLE != c.internalClientFLE && c.metadataClientFLE != c {
if err := c.metadataClientFLE.connect(ctx); err != nil {
if err := c.metadataClientFLE.connect(); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 5b711e8

Please sign in to comment.