// dbs package main import ( "fmt" "log" "os" // "strings" "time" "github.com/garyburd/redigo/redis" "gopkg.in/mgo.v2" ) var ( dbs = Dbs{ RedisURI: "127.0.0.1:6379", Database: "", Collection: "", } ) // Dbs structure type Dbs struct { MongoURI string Database string Collection string RedisURI string rdb *redis.Pool //*redis.Client mdb *mgo.Session } // MongoQuota structure type MongoQuota struct { ID string `json:"_id" bson:"_id"` User string `json:"user" bson:"user"` Messages int `json:"messages" bson:"messages"` Storage int `json:"storage" bson:"storage"` Insert time.Time `json:"insert" bson:"insert"` } func (db *Dbs) isMongodb() bool { if db.MongoURI != "" { return true } return false } func (db *Dbs) poolRedis() { dbs.rdb = &redis.Pool{ MaxIdle: 128, MaxActive: 1000, Wait: true, IdleTimeout: 1 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", db.RedisURI) if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } } func (db *Dbs) connectMongo() { // mongoList := strings.Split(db.MongoURI, ",") // for m := range mongoList { nm, err := mgo.Dial(fmt.Sprintf("mongodb://%s", db.MongoURI)) if err != nil { log.Println("Mongodb connect Error: ", err.Error()) os.Exit(-3) } nm.SetSocketTimeout(5 * time.Second) nm.SetSyncTimeout(5 * time.Second) db.mdb = nm // } }