本文整理汇总了Golang中labix/org/v2/mgo.Session.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Close方法的具体用法?Golang Session.Close怎么用?Golang Session.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Session
的用法示例。
在下文中一共展示了Session.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SaveModel
// Save the DataModel to DataStore
func SaveModel(mgo_db string, m DataModel, conn *mgo.Session) (err error) {
if conn == nil {
conn, err = MgoConnGet(mgo_db)
if err != nil {
return
}
defer conn.Close()
}
if conn != nil {
bsonMid := m.MidGet()
c := conn.DB(mgo_db).C(m.Type())
//Debug("SAVING ", mgo_db, " type=", m.Type(), " Mid=", bsonMid)
if len(bsonMid) < 5 {
m.MidSet(bson.NewObjectId())
if err = c.Insert(m); err != nil {
Log(ERROR, "MGOU ERROR on insert ", err, " TYPE=", m.Type(), " ", m.MidGet())
} else {
//Log(DEBUG, "successfully inserted!!!!!! ", m.MidGet(), " oid=", m.OidGet())
}
} else {
// YOU MUST NOT SEND Mid "_id" to Mongo
mid := m.MidGet()
m.MidSet("") // omitempty means it doesn't get sent
if err = c.Update(bson.M{"_id": bson.ObjectId(bsonMid)}, m); err != nil {
Log(ERROR, "MGOU ERROR on update ", err, " ", bsonMid, " MID=?", m.MidGet())
}
m.MidSet(mid)
}
} else {
Log(ERROR, "MGOU Nil connection")
return errors.New("no db connection")
}
return
}
示例2: Insert
// Save the DataModel to DataStore
func Insert(mgo_db string, m DataModel, conn *mgo.Session) (err error) {
if conn == nil {
conn, err = MgoConnGet(mgo_db)
if err != nil {
return
}
defer conn.Close()
}
if conn != nil {
c := conn.DB(mgo_db).C(m.Type())
if len(m.MidGet()) == 0 {
m.MidSet(bson.NewObjectId())
}
if err = c.Insert(m); err != nil {
Log(ERROR, "MGOU ERROR on insert ", err, " TYPE=", m.Type(), " ", m.MidGet())
} else {
//Log(DEBUG, "successfully inserted!!!!!! ", m.MidGet(), " oid=", m.OidGet())
}
} else {
Log(ERROR, "MGOU Nil connection")
return errors.New("no db connection")
}
return
}
示例3: newServer
func newServer() (*coretesting.MgoInstance, error) {
inst := &coretesting.MgoInstance{Params: []string{"--replSet", name}}
err := inst.Start(true)
if err != nil {
return nil, fmt.Errorf("Error starting mongo server: %s", err.Error())
}
// by dialing right now, we'll wait until it's running
strategy := utils.AttemptStrategy{Total: time.Second * 5, Delay: time.Millisecond * 100}
attempt := strategy.Start()
for attempt.Next() {
var session *mgo.Session
session, err = inst.DialDirect()
if err != nil {
err = fmt.Errorf("Error dialing mongo server %q: %s", inst.Addr(), err.Error())
} else {
session.SetMode(mgo.Monotonic, true)
err = session.Ping()
if err != nil {
err = fmt.Errorf("Error pinging mongo server %q: %s", inst.Addr(), err.Error())
}
session.Close()
}
if err == nil || !attempt.HasNext() {
break
}
}
return inst, err
}
示例4: CloseSession
// 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")
}
示例5: CloseSession
func CloseSession(s *mgo.Session) {
defer func() {
if err := recover(); err != nil {
log.Print("[MGO2_CLOSE_SESSION_RECOVER] close session panic", err)
}
}()
s.Close()
}
示例6: checkServerStatus
// TODO remove dead session from freecon
func (this *Client) checkServerStatus(wg *sync.WaitGroup, sess *mgo.Session) {
defer wg.Done()
err := sess.Ping()
if err != nil {
log.Error("mongodb err: %v %s", sess.LiveServers(), err)
sess.Close()
}
}
示例7: CloseSession
// 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")
}
示例8: insertworker
// insertworker calls func insertdoc for specified insert count
func insertworker(s *mgo.Session, ch chan int) {
c := s.DB("test").C("mongotest")
defer s.Close()
for i := 0; i < Insert_Count; i++ {
err := insertdoc(c, i)
if err != nil {
panic(err)
}
}
ch <- 1
}
示例9: checkServerStatus
func (this *Client) checkServerStatus(wg *sync.WaitGroup, sess *mgo.Session) {
defer wg.Done()
err := sess.Ping()
if err != nil {
// TODO show mongodb uri in log
log.Error("mongodb killed for: %s", err)
sess.Close()
this.killConn(sess)
}
}
示例10: DeleteTodos
func DeleteTodos(resp http.ResponseWriter, req *http.Request,
b *Broker, session *mgo.Session) {
defer session.Close()
var todos []string
if ReadJson(req.Body, &todos) == nil {
collection := TodoCollection(session)
collection.RemoveAll(bson.M{"guid": bson.M{"$in": todos}})
for _, todo := range todos {
message := &Message{"delete", todo}
b.messages <- message
}
}
}
示例11: SaveTodo
func SaveTodo(resp http.ResponseWriter, req *http.Request,
b *Broker, session *mgo.Session) {
defer session.Close()
var todo = new(Todo)
if err := todo.ReadJson(req.Body); err != nil {
http.Error(resp, "Bad Request", http.StatusBadRequest)
} else {
collection := TodoCollection(session)
collection.Upsert(bson.M{"guid": todo.Guid}, todo)
message := &Message{"update", todo.ToJsonString()}
b.messages <- message
}
}
示例12: putFreeConn
func (this *Client) putFreeConn(uri string, sess *mgo.Session) {
this.lk.Lock()
defer this.lk.Unlock()
if this.freeconns == nil {
this.freeconns = make(map[string][]*mgo.Session)
}
freelist := this.freeconns[uri]
if len(freelist) >= this.conf.MaxIdleConnsPerServer {
sess.Close()
return
}
this.freeconns[uri] = append(this.freeconns[uri], sess)
}
示例13: QueueTodos
func QueueTodos(session *mgo.Session, messageChan chan *Message) {
var todos []Todo
defer session.Close()
collection := TodoCollection(session)
iter := collection.Find(nil).Iter()
err := iter.All(&todos)
if err != nil {
return
}
for _, todo := range todos {
message := &Message{"update", todo.ToJsonString()}
messageChan <- message
}
}
示例14: main
// Serve sshkeys at http://:33845/{boxname}
func main() {
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
var session *mgo.Session
var err error
go func() {
for session == nil {
log.Println("Connecting to mongo..")
session, err = mgo.Dial(os.Getenv("CU_DB"))
if err != nil {
if session != nil {
session.Close()
session = nil
}
log.Printf("Database connection failed (%q), retrying..", err)
time.Sleep(10 * time.Second)
}
}
log.Println("Connected to mongo.")
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
u := r.URL.Path[1:]
if session == nil {
log.Printf("%v StatusServiceUnavailable session == nil", u)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
boxUsers, allUsers, keys := getKeys(session, u)
log.Printf("%s:%v:%q:%q", u, strings.Count(keys, "\n"), boxUsers, allUsers)
fmt.Fprint(w, keys)
})
log.Println("Serving..")
err = http.ListenAndServe("localhost:33845", nil)
check(err)
}
示例15: releaseMongo
func releaseMongo(session *mgo.Session, db *mgo.Database) {
mongo, _ := Application.Config["mongo"].(map[string]interface{})
keep_db, ok := mongo["keep_test_db"].(bool)
if !ok {
keep_db = false
}
if !keep_db {
fmt.Println("Testing finished... Dropping database")
db.DropDatabase()
} else {
fmt.Println("Keep test database is true, not dropping database after unit test")
}
session.Close()
}