package mongo import ( "go_sku_server/pkg/config" "gopkg.in/mgo.v2" "strconv" ) var session *mgo.Session type ichuntMongo struct { MongoList map[string]*mgo.Session } var ichuntMongo_ = &ichuntMongo{} func getconn(mongoConfig config.MongoDbDatabase) (*mgo.Session ,error){ url := mongoConfig.Host maxPoolSize := mongoConfig.MaxPoolSize maxPoolSizeInt,err := strconv.Atoi(maxPoolSize) if err != nil{ maxPoolSizeInt = 100 } url += "?maxPoolSize="+maxPoolSize session, err = mgo.Dial(url) if err != nil { return nil ,err } //fmt.Println("url",url) //fmt.Println("maxPoolSizeInt",maxPoolSizeInt) session.SetPoolLimit(maxPoolSizeInt) session.SetMode(mgo.Monotonic, true) myDB :=session.DB(mongoConfig.Database) err = myDB.Login(mongoConfig.UserName,mongoConfig.Password) if err != nil { return nil ,err } return session,nil } func SetUp() (err error) { err = nil ichuntMongo_.MongoList = make(map[string]*mgo.Session,0) mongodbList := config.BuildMongoDbConfgs() if len(mongodbList) > 0{ for mongoName,mongoConfig := range mongodbList{ ichuntMongo_.MongoList[mongoName],err = getconn(mongoConfig) if err != nil{ break } } } return err } func Conn(connection string) (*mgo.Session){ return ichuntMongo_.MongoList[connection].Copy() }