You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/model.go

106 lines
2.9 KiB

package db
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
// "context"
// "fmt"
3 years ago
"github.com/garyburd/redigo/redis"
"gopkg.in/mgo.v2"
3 years ago
"time"
3 years ago
//"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
3 years ago
)
var DB DataBases
type DataBases struct {
3 years ago
MysqlDB mysqlDB
mgoSession *mgo.Session
redisPool *redis.Pool
3 years ago
mongoClient *mongo.Client
}
func key(dbAddress, dbName string) string {
return dbAddress + "_" + dbName
}
func init() {
var mgoSession *mgo.Session
3 years ago
var mongoClient *mongo.Client
var err1 error
3 years ago
//mysql init
3 years ago
initMysqlDB()
// mongo init
3 years ago
// "mongodb://sysop:moon@localhost/records"
// uri := "mongodb://user:pass@sample.host:27017/?maxPoolSize=20&w=majority"
//uri := fmt.Sprintf("mongodb://%s:%s@%s/%s/?maxPoolSize=%d",
// config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword,
// config.Config.Mongo.DBAddress[0],config.Config.Mongo.DBDatabase,
// config.Config.Mongo.DBMaxPoolSize)
//
//mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
//if err != nil{
// log.NewError(" mongo.Connect failed, try ", err.Error(), uri)
// time.Sleep(time.Duration(30) * time.Second)
// mongoClient, err1 = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
// if err1 != nil {
// log.NewError(" mongo.Connect failed, panic", err.Error(), uri)
// panic(err1.Error())
// }
//}
3 years ago
3 years ago
mgoDailInfo := &mgo.DialInfo{
Addrs: config.Config.Mongo.DBAddress,
Direct: config.Config.Mongo.DBDirect,
Timeout: time.Second * time.Duration(config.Config.Mongo.DBTimeout),
Database: config.Config.Mongo.DBDatabase,
Source: config.Config.Mongo.DBSource,
Username: config.Config.Mongo.DBUserName,
Password: config.Config.Mongo.DBPassword,
PoolLimit: config.Config.Mongo.DBMaxPoolSize,
}
mgoSession, err := mgo.DialWithInfo(mgoDailInfo)
3 years ago
if err != nil {
mgoSession, err1 = mgo.DialWithInfo(mgoDailInfo)
if err1 != nil {
// log.NewError(" mongo.Connect failed, panic", err.Error(), uri)
panic(err1.Error())
}
3 years ago
}
3 years ago
DB.mongoClient = mongoClient
3 years ago
DB.mgoSession = mgoSession
DB.mgoSession.SetMode(mgo.Monotonic, true)
c := DB.mgoSession.DB(config.Config.Mongo.DBDatabase).C(cChat)
err = c.EnsureIndexKey("uid")
if err != nil {
panic(err.Error())
}
3 years ago
3 years ago
3 years ago
// redis pool init
DB.redisPool = &redis.Pool{
MaxIdle: config.Config.Redis.DBMaxIdle,
MaxActive: config.Config.Redis.DBMaxActive,
IdleTimeout: time.Duration(config.Config.Redis.DBIdleTimeout) * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial(
"tcp",
config.Config.Redis.DBAddress,
redis.DialReadTimeout(time.Duration(1000)*time.Millisecond),
redis.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
redis.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
redis.DialDatabase(0),
redis.DialPassword(config.Config.Redis.DBPassWord),
)
},
}
}