I'm writing a Go app using mongo-driver to connect to the mongo replica set.
I've noticed that mongo.Connect
is not actually connect to the database.
Even if I've shutdown the mongod
instance, mongo.Connect
sill able to pass through.
However, when I do the query it will connect to the mongod
instance.
Now my problem is I have a lot (>100) concurrent queries to different databases within the same mongod
instance.
The driver create a whole bunch of connections and mongod
failed me Too many files opened
because there is too many connections, Even I use a single mongo.Client
.
Is this a proper behavior of mongo_driver and how can I deal with this?
Does MongoDB require each connection per each database?
mongo.Connect()
creates a new mongo.Client
and initializes it, but does not (necessarily) create connections to the DB server.
To actually create a connection and check if the server is reachable (without executing a query), you may use the Client.Ping()
method. This will return an error if the server is not reachable.
The official mongodb driver manages an internal pool of connections. Connections are not closed immediately after use, instead they are put back into the pool so when a connection is needed to carry out an operation, an idle connection can be used from the pool immediately. This is the intended behavior. You may configure its size via the options.ClientOptions
you pass to mongo.Connect()
.
See ClientOptions.SetMaxPoolSize()
:
SetMaxPoolSize specifies that maximum number of connections allowed in the driver's connection pool to each server. Requests to a server will block if this maximum is reached. This can also be set through the "maxPoolSize" URI option (e.g. "maxPoolSize=100"). The default is 100. If this is 0, it will be set to math.MaxInt64.
Example setting up a client with limited connections, and pinging it:
ctx := context.Background()
opts := options.Client().
ApplyURI("mongodb://localhost").
SetMaxPoolSize(20) // Allow no more than 20 connections per server
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Printf("mongo.Connect() failed: %v", err)
return
}
defer client.Disconnect(ctx)
if err := client.Ping(ctx, nil); err != nil {
log.Printf("Can't connect to db: %v", err)
return
}
// Use client
See related: goroutine create multiple mongodb connection