Files
open-im-server/pkg/common/db/model.go
T

149 lines
4.5 KiB
Go
Raw Normal View History

2021-05-26 19:17:51 +08:00
package db
import (
"Open_IM/pkg/common/config"
2022-04-18 19:24:36 +08:00
"go.mongodb.org/mongo-driver/bson"
2022-04-05 19:31:35 +08:00
//"Open_IM/pkg/common/log"
2022-02-23 12:27:01 +08:00
"Open_IM/pkg/utils"
"fmt"
"go.mongodb.org/mongo-driver/mongo/options"
2022-02-23 12:00:02 +08:00
// "context"
// "fmt"
2021-06-28 15:32:26 +08:00
"github.com/garyburd/redigo/redis"
2021-05-26 19:17:51 +08:00
"gopkg.in/mgo.v2"
2021-06-28 15:32:26 +08:00
"time"
2022-02-21 15:01:06 +08:00
2022-02-23 12:27:01 +08:00
"context"
2022-02-21 15:01:06 +08:00
//"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
2022-02-23 12:00:02 +08:00
// "go.mongodb.org/mongo-driver/mongo/options"
2021-05-26 19:17:51 +08:00
)
var DB DataBases
type DataBases struct {
2022-03-17 11:55:35 +08:00
MysqlDB mysqlDB
mgoSession *mgo.Session
redisPool *redis.Pool
2022-02-21 15:01:06 +08:00
mongoClient *mongo.Client
2021-05-26 19:17:51 +08:00
}
func key(dbAddress, dbName string) string {
return dbAddress + "_" + dbName
}
func init() {
2022-04-05 19:31:35 +08:00
//log.NewPrivateLog(constant.LogFileName)
2022-02-21 15:01:06 +08:00
var mongoClient *mongo.Client
2021-11-26 14:34:00 +08:00
var err1 error
2021-06-28 15:32:26 +08:00
//mysql init
2021-07-15 11:14:07 +08:00
initMysqlDB()
2021-11-11 19:04:35 +08:00
// mongo init
2022-02-21 15:01:06 +08:00
// "mongodb://sysop:moon@localhost/records"
2022-02-23 14:30:21 +08:00
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
2022-03-21 11:13:17 +08:00
if config.Config.Mongo.DBUri != "" {
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
uri = config.Config.Mongo.DBUri
} else {
2022-04-15 11:56:36 +08:00
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
2022-04-15 18:16:28 +08:00
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress[0],
2022-04-15 11:56:36 +08:00
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
2022-04-15 17:43:04 +08:00
} else {
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d",
config.Config.Mongo.DBAddress[0], config.Config.Mongo.DBDatabase,
config.Config.Mongo.DBMaxPoolSize)
2022-04-15 11:56:36 +08:00
}
2022-03-21 11:13:17 +08:00
}
2022-02-23 12:27:01 +08:00
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
2022-03-31 11:28:51 +08:00
if err != nil {
2022-04-05 19:31:35 +08:00
fmt.Println(" mongo.Connect failed, try ", utils.GetSelfFuncName(), err.Error(), uri)
2022-02-23 12:27:01 +08:00
time.Sleep(time.Duration(30) * time.Second)
mongoClient, err1 = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err1 != nil {
2022-04-05 19:31:35 +08:00
fmt.Println(" mongo.Connect retry failed, panic", err.Error(), uri)
2022-02-23 12:27:01 +08:00
panic(err1.Error())
}
}
2022-04-15 17:43:04 +08:00
fmt.Println("0", utils.GetSelfFuncName(), "mongo driver client init success: ", uri)
2022-04-18 19:24:36 +08:00
// mongodb create index
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
dataBase := mongoClient.Database(config.Config.Mongo.DBDatabase)
2022-04-05 19:31:35 +08:00
2022-04-18 19:24:36 +08:00
cCommentMsgModels := []mongo.IndexModel{
{
Keys: bson.M{"create_time": -1, "user_id": -1},
},
}
result, err := dataBase.Collection(cCommentMsg).Indexes().CreateMany(context.Background(), cCommentMsgModels, opts)
if err != nil {
fmt.Println("mongodb create cCommentMsgModels failed", result, err.Error())
}
2021-06-28 15:32:26 +08:00
2022-04-18 19:24:36 +08:00
cSendLogModels := []mongo.IndexModel{
{
Keys: bson.M{"user_id": -1, "send_time": -1},
},
}
result, err = dataBase.Collection(cSendLog).Indexes().CreateMany(context.Background(), cSendLogModels, opts)
if err != nil {
fmt.Println("mongodb create cSendLogModels failed", result, err.Error())
}
2022-02-23 12:30:18 +08:00
2022-04-18 19:24:36 +08:00
cChatModels := []mongo.IndexModel{
{
Keys: bson.M{"uid": -1},
},
}
result, err = dataBase.Collection(cChat).Indexes().CreateMany(context.Background(), cChatModels, opts)
if err != nil {
fmt.Println("mongodb create cChatModels failed", result, err.Error())
}
cWorkMomentModels := []mongo.IndexModel{
{
Keys: bson.M{"work_moment_id": -1},
},
{
Keys: bson.M{"user_id": -1, "create_time": -1},
},
}
result, err = dataBase.Collection(cWorkMoment).Indexes().CreateMany(context.Background(), cWorkMomentModels, opts)
if err != nil {
fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error())
}
cTagModels := []mongo.IndexModel{
{
Keys: bson.M{"tag_id": -1},
},
{
Keys: bson.M{"user_id": -1, "tag_id": -1},
},
}
result, err = dataBase.Collection(cTag).Indexes().CreateMany(context.Background(), cTagModels, opts)
if err != nil {
fmt.Println("mongodb create cTagModels failed", result, err.Error())
}
DB.mongoClient = mongoClient
2022-02-21 15:01:06 +08:00
2021-06-28 15:32:26 +08:00
// 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),
)
},
}
2021-05-26 19:17:51 +08:00
}