// CloseSession puts the connection back into the pool func CloseSession(sessionId string, mongoSession *mgo.Session) { defer helper.CatchPanic(nil, sessionId, "CloseSession") tracelog.STARTED(sessionId, "CloseSession") mongoSession.Close() tracelog.COMPLETED(sessionId, "CloseSession") }
// Shutdown systematically brings the manager down gracefully func Shutdown(sessionId string) (err error) { defer helper.CatchPanic(&err, sessionId, "Shutdown") tracelog.STARTED(sessionId, "Shutdown") // Close the databases for _, session := range singleton.sessions { CloseSession(sessionId, session.mongoSession) } tracelog.COMPLETED(sessionId, "Shutdown") return err }
// CreateSession creates a connection pool for use func CreateSession(sessionId string, mode string, sessionName string, hosts []string, databaseName string, username string, password string) (err error) { defer helper.CatchPanic(nil, sessionId, "CreateSession") tracelog.STARTEDf(sessionId, "CreateSession", "Mode[%s] SessionName[%s] Hosts[%s] DatabaseName[%s] Username[%s]", mode, sessionName, hosts, databaseName, username) // Create the database object mongoSession := &mongoSession{ mongoDBDialInfo: &mgo.DialInfo{ Addrs: hosts, Timeout: 60 * time.Second, Database: databaseName, Username: username, Password: password, }, } // Establish the master session mongoSession.mongoSession, err = mgo.DialWithInfo(mongoSession.mongoDBDialInfo) if err != nil { tracelog.COMPLETED_ERROR(err, sessionId, "CreateSession") return err } switch mode { case "strong": // Reads and writes will always be made to the master server using a // unique connection so that reads and writes are fully consistent, // ordered, and observing the most up-to-date data. // http://godoc.org/labix.org/v2/mgo#Session.SetMode mongoSession.mongoSession.SetMode(mgo.Strong, true) break case "monotonic": // Reads may not be entirely up-to-date, but they will always see the // history of changes moving forward, the data read will be consistent // across sequential queries in the same session, and modifications made // within the session will be observed in following queries (read-your-writes). // http://godoc.org/labix.org/v2/mgo#Session.SetMode mongoSession.mongoSession.SetMode(mgo.Monotonic, true) } // Have the session check for errors // http://godoc.org/labix.org/v2/mgo#Session.SetSafe mongoSession.mongoSession.SetSafe(&mgo.Safe{}) // Add the database to the map singleton.sessions[sessionName] = mongoSession tracelog.COMPLETED(sessionId, "CreateSession") return err }
// CloneSession makes a clone of the specified session for client use func CloneSession(sessionId string, useSession string) (mongoSession *mgo.Session, err error) { defer helper.CatchPanic(nil, sessionId, "CopySession") tracelog.STARTEDf(sessionId, "CloneSession", "UseSession[%s]", useSession) // Find the session object session := singleton.sessions[useSession] if session == nil { err = fmt.Errorf("Unable To Locate Session %s", useSession) tracelog.COMPLETED_ERROR(err, sessionId, "CloneSession") return mongoSession, err } // Clone the master session mongoSession = session.mongoSession.Clone() tracelog.COMPLETED(sessionId, "CloneSession") return mongoSession, err }
// Startup brings the manager to a running state func Startup(sessionId string) (err error) { defer helper.CatchPanic(&err, sessionId, "Startup") // If the system has already been started ignore the call if singleton != nil { return err } tracelog.STARTED(sessionId, "Startup") // Pull in the configuration config := mongoConfiguration{} err = envconfig.Process("mgo", &config) if err != nil { tracelog.COMPLETED_ERROR(err, sessionId, "Startup") return err } // Create the Mongo Manager singleton = &mongoManager{ sessions: map[string]*mongoSession{}, } // Log the mongodb connection straps tracelog.TRACE(sessionId, "Startup", "MongoDB : Hosts[%s]", config.Hosts) tracelog.TRACE(sessionId, "Startup", "MongoDB : Database[%s]", config.Database) tracelog.TRACE(sessionId, "Startup", "MongoDB : Username[%s]", config.UserName) hosts := strings.Split(config.Hosts, ",") // Create the strong and monotonic sessions err = CreateSession(sessionId, "strong", MASTER_SESSION, hosts, config.Database, config.UserName, config.Password) err = CreateSession(sessionId, "monotonic", MONOTONIC_SESSION, hosts, config.Database, config.UserName, config.Password) tracelog.COMPLETED(sessionId, "Startup") return err }
// CatchPanicNoErr is used to stop and process panics before they reach the Go runtime. func (baseController *BaseController) CatchPanicNoErr(UUID string, functionName string) { if helper.CatchPanic(nil, UUID, functionName) { baseController.ServeAppError() } }