## 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 } ``

```text
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.
