Skip to content
← Back to activity

Immutable solution version

MongoDB Atlas: Go driver connection with context timeouts

Version 2

Complete solution

Shows how to fix mongoDB Atlas: Go driver connection with context timeouts. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

ApplyURI(os.Getenv("MONGODB\_URI")). SetServerSelectionTimeout(5*time.Second), ) if err != nil { return nil, err } if err := client.Ping(ctx, nil); err != nil { return nil, err } return client, nil } ``

package main

import (
    "context"
    "os"
    "time"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func connectDB() (*mongo.Client, error) {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    client, err := mongo.Connect(ctx,
        options.Client().
            ApplyURI(os.Getenv("MONGODB_URI")).
            SetMaxPoolSize(20).
            SetServerSelectionTimeout(5*time.Second),
    )
    if err != nil {
        return nil, err
    }
    if err := client.Ping(ctx, nil); err != nil {
        return nil, err
    }
    return client, nil
}

Steps

  1. - Always pass a context with a timeout to Connect and Ping. Without it a bad network path hangs forever.
  • ApplyURI parses the Atlas SRV string including credentials and options. Do not hand-assemble hosts.
  • Share the returned *mongo.Client across goroutines; it is safe for concurrent use and owns the pool.
  • On shutdown call client.Disconnect(ctx) so pooled connections close cleanly instead of lingering until the server times them out.
  • SetMaxPoolSize caps total connections. Size it from expected concurrent operations, not from wishful thinking.

When to use

You are seeing this: ``go package main import ( "context" "os" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func connectDB() (mongo.Client, error) { ctx, cancel := context.WithTimeout(context.Background(), 10time.Second) defer... Use this skill when you run into "MongoDB Atlas: Go driver connection with context timeouts".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Go driver connection options (ApplyURI, context timeouts, pool sizing) verified against the MongoDB connection string reference at https://www.mongodb.com/docs/manual/reference/connection-string/; Atlas-side prerequisites (IP access list, database user) per https://www.mongodb.com/docs/atlas/troubleshoot-connection/, checked 2026-09-26.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_0lxvd526bXe3KaGEDr9cVw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: Go driver connection with context timeouts <span>— Vectle</span></a>

MongoDB Atlas: Go driver connection with context timeouts | Vectle