1

My go code is making a connection to MongoDB like this:

uri := "mongodb://localhost:27017"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyUIR(uri))
if err != nil {
        panic(err)
}

I want to create a couple of goroutines that regularly queries the database, and not sure if making every goroutine uses client is safe. What would be the most conventional way to make multiple goroutines interact with the same database?

1 Answer 1

2

Single client instance is safe to use in multiple goroutines. From the docs:

Client is a handle representing a pool of connections to a MongoDB deployment. It is safe for concurrent use by multiple goroutines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.