In the article running-mongodb-queries-concurrently-with-go said that mgo.DialWithInfo : Create a session which maintains a pool of socket connections to MongoDB,
Looking into the source code for the Dial function calls, you can see that the Dial
function calls the DialWithTimeout
function which calls the DialWithInfo
function. So to answer your question about the differences between the functions, it seems like Dial
is a convenience wrapper for DialWithTimeout
, which in turn is a convenience wrapper for DialWithInfo
, so they result in the same connection pool.
As to how to manage that connection pool, you've got it right in your question.
Further sessions to the same cluster are then established using the New or Copy methods on the obtained session. This will make them share the underlying cluster, and manage the pool of connections appropriately.
So a single call to Dial
or DialWithTimeout
or DialWithInfo
will establish the connection pool, if you require more than one session, use the session.New()
or session.Copy()
methods to obtain it from the session returned from whichever Dial function you chose to use.