mongodb - Concurrently using the same mgo session in go -


so i'm having trouble figuring out best practices using concurrency mongodb in go. first implementation of getting session looked this:

var globalsession *mgo.session  func getsession() (*mgo.session, error) {     //establish our database connection     if globalsession == nil {         var err error         globalsession, err = mgo.dial(":27017")         if err != nil {             return nil, err         }          //optional. switch session monotonic behavior.         globalsession.setmode(mgo.monotonic, true)     }      return globalsession.copy(), nil } 

this works great trouble i'm running mongo has limit of 204 connections starts refusing connections connection refused because many open connections: 204;however, issue since i'm calling session.copy() returns session , not error. event though connection refused program never thrown error.

now though doing having 1 session , using instead of copy can have access connection error so:

var session *mgo.session = nil  func newsession() (*mgo.session, error) {     if session == nil {         session, err = mgo.dial(url)         if err != nil {             return nil, err         }     }      return session, nil } 

now problem have don't know happen if try make concurrent usage of same session.


Comments