title: Using the MongoDB Go Driver, how do I set up connection pooling?
body: <p>I'm using the <a href="https://github.com/mongodb/mongo-go-driver" rel="noreferrer">MongoDB Go Driver</a> in my Go (1.11) server which runs on Google Cloud's App Engine. I'm not really sure if I still manually have to set up connection pooling or if it's already being taken care of out of the box. For example I'm not entirely sure what the context (with timeout) exactly means.</p>
  
  <p>My code looks like this:</p>
  
  <pre class="lang-golang prettyprint-override"><code>package tools
  
  import (
      "context"
      "time"
      "valuation-app/settings"
  
      "go.mongodb.org/mongo-driver/mongo"
      "go.mongodb.org/mongo-driver/mongo/options"
  )
  
  // ConnectToDB starts a new database connection and returns a reference to it
  func ConnectToDB() (*mongo.Database, error) {
      settings := settings.Get().Database
      ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
      defer cancel()
      options := options.Client().ApplyURI(settings.URI)
      options.SetMaxPoolSize(10)
      client, err := mongo.Connect(ctx, options)
      if err != nil {
          return nil, err
      }
  
      return client.Database(settings.DatabaseName), nil
  }
  </code></pre>