mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-08 19:16:35 +08:00
group
This commit is contained in:
Vendored
+60
-25
@@ -50,20 +50,14 @@ type Cache interface {
|
||||
|
||||
// native redis operate
|
||||
|
||||
func NewRedis() *RedisClient {
|
||||
o := &RedisClient{}
|
||||
o.InitRedis()
|
||||
return o
|
||||
}
|
||||
//func NewRedis() *RedisClient {
|
||||
// o := &RedisClient{}
|
||||
// o.InitRedis()
|
||||
// return o
|
||||
//}
|
||||
|
||||
type RedisClient struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func (r *RedisClient) InitRedis() {
|
||||
func NewRedis() (*RedisClient, error) {
|
||||
var rdb redis.UniversalClient
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
if config.Config.Redis.EnableCluster {
|
||||
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: config.Config.Redis.DBAddress,
|
||||
@@ -71,11 +65,10 @@ func (r *RedisClient) InitRedis() {
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
PoolSize: 50,
|
||||
})
|
||||
_, err = rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
fmt.Println("redis cluster failed address ", config.Config.Redis.DBAddress)
|
||||
panic(err.Error() + " redis cluster " + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
}
|
||||
//if err := rdb.Ping(ctx).Err();err != nil {
|
||||
// return nil, fmt.Errorf("redis ping %w", err)
|
||||
//}
|
||||
//return &RedisClient{rdb: rdb}, nil
|
||||
} else {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: config.Config.Redis.DBAddress[0],
|
||||
@@ -84,21 +77,63 @@ func (r *RedisClient) InitRedis() {
|
||||
DB: 0, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
_, err = rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
}
|
||||
//err := rdb.Ping(ctx).Err()
|
||||
//if err != nil {
|
||||
// panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
//}
|
||||
}
|
||||
r.rdb = rdb
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
err := rdb.Ping(ctx).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("redis ping %w", err)
|
||||
}
|
||||
return &RedisClient{rdb: rdb}, nil
|
||||
}
|
||||
|
||||
type RedisClient struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
//func (r *RedisClient) InitRedis() {
|
||||
// var rdb redis.UniversalClient
|
||||
// var err error
|
||||
// ctx := context.Background()
|
||||
// if config.Config.Redis.EnableCluster {
|
||||
// rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
// Addrs: config.Config.Redis.DBAddress,
|
||||
// Username: config.Config.Redis.DBUserName,
|
||||
// Password: config.Config.Redis.DBPassWord, // no password set
|
||||
// PoolSize: 50,
|
||||
// })
|
||||
// _, err = rdb.Ping(ctx).Result()
|
||||
// if err != nil {
|
||||
// fmt.Println("redis cluster failed address ", config.Config.Redis.DBAddress)
|
||||
// panic(err.Error() + " redis cluster " + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
// }
|
||||
// } else {
|
||||
// rdb = redis.NewClient(&redis.Options{
|
||||
// Addr: config.Config.Redis.DBAddress[0],
|
||||
// Username: config.Config.Redis.DBUserName,
|
||||
// Password: config.Config.Redis.DBPassWord, // no password set
|
||||
// DB: 0, // use default DB
|
||||
// PoolSize: 100, // 连接池大小
|
||||
// })
|
||||
// _, err = rdb.Ping(ctx).Result()
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
// }
|
||||
// }
|
||||
// r.rdb = rdb
|
||||
//}
|
||||
|
||||
func (r *RedisClient) GetClient() redis.UniversalClient {
|
||||
return r.rdb
|
||||
}
|
||||
|
||||
func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
|
||||
return &RedisClient{rdb: rdb}
|
||||
}
|
||||
//func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
|
||||
// return &RedisClient{rdb: rdb}
|
||||
//}
|
||||
|
||||
// Perform seq auto-increment operation of user messages
|
||||
func (r *RedisClient) IncrUserSeq(uid string) (uint64, error) {
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func NewGormDB() (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
|
||||
db, err := gorm.Open(mysql.Open(dsn), nil)
|
||||
if err != nil {
|
||||
time.Sleep(time.Duration(30) * time.Second)
|
||||
db, err = gorm.Open(mysql.Open(dsn), nil)
|
||||
if err != nil {
|
||||
panic(err.Error() + " open failed " + dsn)
|
||||
}
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer sqlDB.Close()
|
||||
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
|
||||
err = db.Exec(sql).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init db %w", err)
|
||||
}
|
||||
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
newLogger := logger.New(
|
||||
Writer{},
|
||||
logger.Config{
|
||||
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
Colorful: true, // Disable color
|
||||
},
|
||||
)
|
||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDB, err = db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
||||
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
type Mysql struct {
|
||||
gormConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *Mysql) GormConn() *gorm.DB {
|
||||
return m.gormConn
|
||||
}
|
||||
|
||||
//func (m *Mysql) SetGormConn(gormConn *gorm.DB) {
|
||||
// m.gormConn = gormConn
|
||||
//}
|
||||
//
|
||||
//func (m *Mysql) InitConn() *Mysql {
|
||||
// dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
// config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
|
||||
// var db *gorm.DB
|
||||
// db, err := gorm.Open(mysql.Open(dsn), nil)
|
||||
// if err != nil {
|
||||
// time.Sleep(time.Duration(30) * time.Second)
|
||||
// db, err = gorm.Open(mysql.Open(dsn), nil)
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " open failed " + dsn)
|
||||
// }
|
||||
// }
|
||||
// sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
|
||||
// err = db.Exec(sql).Error
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " Exec failed:" + sql)
|
||||
// }
|
||||
// dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
// config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
// newLogger := logger.New(
|
||||
// Writer{},
|
||||
// logger.Config{
|
||||
// SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
// LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
// IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
// Colorful: true, // Disable color
|
||||
// },
|
||||
// )
|
||||
// db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
// Logger: newLogger,
|
||||
// })
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " Open failed " + dsn)
|
||||
// }
|
||||
// sqlDB, err := db.DB()
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " DB.DB() failed ")
|
||||
// }
|
||||
// sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
||||
// sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
||||
// sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
||||
// if db == nil {
|
||||
// panic("db is nil")
|
||||
// }
|
||||
// m.SetGormConn(db)
|
||||
// return m
|
||||
//}
|
||||
|
||||
//models := []interface{}{&Friend{}, &FriendRequest{}, &Group{}, &GroupMember{}, &GroupRequest{},
|
||||
// &User{}, &Black{}, &ChatLog{}, &Conversation{}, &AppVersion{}}
|
||||
|
||||
//func (m *Mysql) AutoMigrateModel(model interface{}) error {
|
||||
// err := m.gormConn.AutoMigrate(model)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// m.gormConn.Set("gorm:table_options", "CHARSET=utf8")
|
||||
// m.gormConn.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
||||
// _ = m.gormConn.Migrator().CreateTable(model)
|
||||
// return nil
|
||||
//}
|
||||
|
||||
type Writer struct{}
|
||||
|
||||
func (w Writer) Printf(format string, args ...interface{}) {
|
||||
fmt.Printf(format, args...)
|
||||
}
|
||||
|
||||
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
|
||||
if len(tx) > 0 {
|
||||
if txDB, ok := tx[0].(*gorm.DB); ok {
|
||||
return txDB
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
@@ -13,10 +13,45 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewMongo() *Mongo {
|
||||
mgo := &Mongo{}
|
||||
mgo.InitMongo()
|
||||
return mgo
|
||||
//func NewMongo() *Mongo {
|
||||
// mgo := &Mongo{}
|
||||
// mgo.InitMongo()
|
||||
// return mgo
|
||||
//}
|
||||
|
||||
func NewMongo() (*Mongo, error) {
|
||||
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
||||
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 {
|
||||
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
|
||||
mongodbHosts := ""
|
||||
for i, v := range config.Config.Mongo.DBAddress {
|
||||
if i == len(config.Config.Mongo.DBAddress)-1 {
|
||||
mongodbHosts += v
|
||||
} else {
|
||||
mongodbHosts += v + ","
|
||||
}
|
||||
}
|
||||
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
|
||||
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
|
||||
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
||||
} else {
|
||||
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
||||
mongodbHosts, config.Config.Mongo.DBDatabase,
|
||||
config.Config.Mongo.DBMaxPoolSize)
|
||||
}
|
||||
}
|
||||
fmt.Println("mongo:", uri)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||
defer cancel()
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Mongo{db: mongoClient}, nil
|
||||
}
|
||||
|
||||
type Mongo struct {
|
||||
|
||||
Reference in New Issue
Block a user