mgo.Session is safe to use at the same time. Quote from the document:
All concurrency session methods are safe and can be called from multiple goroutines.
But this does not mean that you should not create and use more of them in parallel by calling Session.Copy() or Session.Clone() , in the initial session received during dialing.
To be concurrency - safe and take advantage of using more of them, are not mutually exclusive (they are not mutually exclusive). Although you can use a single mgo.Session from an arbitrary number of goroutines that will not scale well, it will not scale at all. Sessions automatically manage the connection pool, possibly even for multiple server nodes, but if you use one Session , you do not use it. By creating a new Session at the beginning of each of your requests (if necessary) and closing it correctly at the end ( Session.Close() , preferably called using defer ), you use the potential use of several connections at the same time, possibly on several servers (if any) and therefore it is better to use server resources; and getting faster response times (both from the database and ultimately to your end-users HTTP). Calling Session.Close() does not close the basic connection to the server, it will simply return the connection back to the pool, ready to pick up another session.
Also see the related question about using Session s: Performance of mgo requests looks consistently slow (500-650 ms)
icza
source share