Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1031

mongo.Connect takes a context for no purpose

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Connections, Documentation
    • Labels:
      None

      The mongo.Connect function https://godoc.org/go.mongodb.org/mongo-driver/mongo#Connect takes a context. However it does not seem to do anything with it: https://github.com/mongodb/mongo-go-driver/blob/master/mongo/client.go#L101

       

      By taking context, it leads one to believe that the connect method should be request-scoped as per Golang's official documentation https://golang.org/pkg/context/ . Instead, according to mongo's tutorial https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial it says 

      It is best practice to keep a client that is connected to MongoDB around so that the application can make use of connection pooling - you don't want to open and close a connection for each query. However, if your application no longer requires a connection, the connection can be closed with client.Disconnect() like so:

      You can see how the documentation and the use of context creates a really confusing understanding of where the client.Connect() method is supposed to live and how it's supposed to be handled.

       

      First I realized that using client.Connect() in a request scope will cause an error because there can be multiple requests and client.Connect() returns an error if the client is already connected. Then I looked at the source code and realized context isn't actually being used.

      Now I connect once with an ad-hoc context, and share that connection for all requests. I believe that is the correct usage. But the documentation and tutorial does not make it clear

       

      Request: Connect should not take a context unless it is supposed to be request scoped and unless it is being utilized in someway. Documentation and tutorials should make it more clear where an app should Connect

            Assignee:
            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            Reporter:
            qhenkart@gmail.com Quest Henkart
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: