INNER CODE UNIT · Go
create
aliez-ren/mongood · go/main.go:61
func create(uri string) (*mongo.Client, error) {
if cached, ok := clients.Load(uri); ok && cached != nil {
return cached.(*mongo.Client), nil
}
// Use mutex to make sure there is only one active mongodb client instance for one uri.
// While with mutex, clients for different mongodb servers must be created one by one.
creationMutex.Lock()
defer creationMutex.Unlock()
// check again, if it is already created, just return.
if cached, ok := clients.Load(uri); ok && cached != nil {
return cached.(*mongo.Client), nil
}
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
return nil, err