// GoogleNewsFindIfExist check google news current data if exist before insert func GoogleNewsFindIfExist(URL string, sc *mongodb.Session) bool { c := sc.DB(Db).C(googleNewsCollection) var result map[string]interface{} c.Find(bson.M{"url": URL}).One(&result) if result["url"] != nil && result["url"] != URL { return false } return true }
// GetDatabaseStatus returns stats for a given database func GetDatabaseStatus(session *mgo.Session, db string) *DatabaseStatus { dbStatus := &DatabaseStatus{} err := session.DB(db).Run(bson.D{{"dbStats", 1}, {"scale", 1}}, &dbStatus) if err != nil { glog.Error(err) return nil } return dbStatus }
func TestConn(t *testing.T) { fmt.Println(MInit("mongodb://127.0.0.1:27017/test", "test", "t")) for { var s *mgo.Session = GetMConn("t") e := []map[string]interface{}{} s.DB("admin").C("system.users").Find(bson.M{}).All(&e) b, _ := json.Marshal(e) fmt.Println(string(b)) time.Sleep(time.Second * 5) } }
func GetDatabase(databaseHost string) (database.Interface, error) { var session *mgo.Session var err error if session, err = getSession(databaseHost); err != nil { return nil, err } database := &database.MongoDB{Collection: session.DB("steamfetcher").C("games")} database.SetSession(session) return database, nil }
func MInit(uri, dbName, k string) error { DB_NAME = dbName n := time.Second var e error var m_conn *mgo.Session = mcMap[k] for { if session, err := mgo.Dial(uri); err != nil { e = err } else { m_conn = session if err := m_conn.Ping(); err != nil { e = err } else { break } } fmt.Printf("fail to connect mysql,err:%v,uri:%s,try to reconnect in %d second\n", e, uri, n/1000000000) time.Sleep(n) n = n * 2 //最大重连间隔时长:10分钟 if n > 600*time.Second { n = 600 * time.Second } } mcMap[k] = m_conn return nil }
func add(session *mgo.Session, gender models.Gender, record Record) { c := session.DB("").C(models.Collection) c.EnsureIndex(mgo.Index{ Key: []string{"name", "gender"}, Unique: true, }) name := &models.Name{} c.Find(bson.M{"name": record.Name, "gender": gender}).One(&name) if name.Id == "" { name.Id = bson.NewObjectId() name.Name = record.Name name.Gender = gender } name.Rank.Set(record.Year, record.Rank) name.Count.Set(record.Year, record.Count) c.UpsertId(name.Id, bson.M{"$set": name}) }
func getSession(databaseHost string) (*mgo.Session, error) { var session *mgo.Session var err error if session, err = mgo.DialWithTimeout(databaseHost, time.Second); err != nil { return nil, err } session.SetSocketTimeout(time.Second) session.SetSyncTimeout(time.Second) session.SetMode(mgo.Monotonic, true) return session, nil }
func GetMConn(k string) *mgo.Session { var m_conn *mgo.Session = getMConn(k) var n = time.Second for { if err := m_conn.Ping(); err != nil { fmt.Printf("mongodb断开连接,%d秒后开始重连", n/time.Second) m_conn.Refresh() } else { if n/time.Second > 1 { fmt.Printf("重连成功") } break } fmt.Println("正在尝试重连") time.Sleep(n) n = n * 2 //最大重连间隔时长:10分钟 if n > 600*time.Second { n = 600 * time.Second } } return m_conn }
"github.com/henrylee2cn/pholcus/config" "github.com/henrylee2cn/pholcus/logs" ) type MgoSrc struct { *mgo.Session } var ( connGcSecond = time.Duration(config.MGO_CONN_GC_SECOND) * 1e9 session *mgo.Session err error MgoPool = pool.ClassicPool( config.MGO_CONN_CAP, config.MGO_CONN_CAP/5, func() (pool.Src, error) { // if err != nil || session.Ping() != nil { // session, err = newSession() // } return &MgoSrc{session.Clone()}, err }, connGcSecond) ) func Refresh() { session, err = mgo.Dial(config.MGO_CONN_STR) if err != nil { logs.Log.Error("MongoDB:%v\n", err) } else if err = session.Ping(); err != nil { logs.Log.Error("MongoDB:%v\n", err) } else {
// Remove testing database and close session func RandomTestMongoDown(session *mgo.Session, dbName string) { session.DB(dbName).DropDatabase() session.Close() }
func CopySession(session *mgo.Session) *mgo.Session { return session.Copy() }
func Collection(s *mgo.Session) *mgo.Collection { return s.DB("test").C("books") }
func GetDb(se *mgo.Session) *mgo.Database { return se.DB(DbCfg.Name) }