mongodb库升级包
This commit is contained in:
parent
5c38610b55
commit
d9f0c18658
|
@ -0,0 +1,29 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var paths = []string{
|
||||
".",
|
||||
"./etc",
|
||||
"./config",
|
||||
}
|
||||
|
||||
func GetViper(name, filetype string) *viper.Viper {
|
||||
vp := viper.New()
|
||||
// 配置文件
|
||||
vp.SetConfigName(name)
|
||||
vp.SetConfigType(filetype)
|
||||
for _, v := range paths {
|
||||
vp.AddConfigPath(v)
|
||||
}
|
||||
|
||||
err := vp.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("fatal error config file: %w", err))
|
||||
}
|
||||
return vp
|
||||
}
|
|
@ -9,8 +9,23 @@ import (
|
|||
newMongo "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"mongo.games.com/goserver/core/logger"
|
||||
|
||||
"mongo.games.com/game/common"
|
||||
"mongo.games.com/game/mongo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 读取配置文件
|
||||
vp := common.GetViper("mgo", "json")
|
||||
// mongo初始化
|
||||
conf := &mongo.Config{}
|
||||
err := vp.Unmarshal(conf)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("mongo config error: %v", err))
|
||||
}
|
||||
mongo.Init(conf)
|
||||
}
|
||||
|
||||
var globalMongoSession *newMongo.Client
|
||||
|
||||
func mongoURI(user, password, host string, port int32, options string) string {
|
||||
|
@ -31,6 +46,8 @@ func mongoURI(user, password, host string, port int32, options string) string {
|
|||
return url
|
||||
}
|
||||
|
||||
// NewMongoClient 创建mongo客户端
|
||||
// Deprecated: use [mongo.games.com/game/mongo] instead
|
||||
func NewMongoClient() (*newMongo.Client, error) {
|
||||
if globalMongoSession != nil {
|
||||
return globalMongoSession, nil
|
||||
|
|
|
@ -2,6 +2,8 @@ package mongo
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"mongo.games.com/game/mongo/internal"
|
||||
"mongo.games.com/goserver/core/logger"
|
||||
)
|
||||
|
@ -9,6 +11,7 @@ import (
|
|||
type Config = internal.Config
|
||||
type DatabaseConfig = internal.DatabaseConfig
|
||||
type Collection = internal.Collection
|
||||
type Database = internal.Database
|
||||
|
||||
var _manager *internal.Manager
|
||||
var _conf *internal.Config
|
||||
|
@ -19,10 +22,9 @@ func GetConfig() *Config {
|
|||
}
|
||||
|
||||
// Init 初始化
|
||||
func Init(conf *Config) error {
|
||||
func Init(conf *Config) {
|
||||
_conf = conf
|
||||
_manager = internal.NewManager(_conf)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart 重启
|
||||
|
@ -34,6 +36,11 @@ func Restart() {
|
|||
_manager.Restart(_conf)
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func Close() {
|
||||
internal.Close(_manager)
|
||||
}
|
||||
|
||||
// GetGlobalCollection 获取全局库
|
||||
// database: 数据库名称
|
||||
// collection: 集合名称
|
||||
|
@ -56,3 +63,18 @@ func GetCollection(platform, database, collection string) (*Collection, error) {
|
|||
|
||||
return _manager.GetCollection(platform, database, collection)
|
||||
}
|
||||
|
||||
// GetClient 获取数据库连接
|
||||
// 默认获取的是 Global, log 的数据库连接
|
||||
func GetClient() (*mongo.Client, error) {
|
||||
if _manager == nil {
|
||||
return nil, errors.New("mongo manager is nil, please call Init() first")
|
||||
}
|
||||
|
||||
c, err := _manager.GetCollection("global", "log", "empty")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.Database.Client, nil
|
||||
}
|
||||
|
|
|
@ -143,28 +143,9 @@ func (m *Manager) GetCollection(key, database, collection string) (*Collection,
|
|||
|
||||
func (m *Manager) Restart(conf *Config) {
|
||||
logger.Logger.Infof("mongo manager restart...")
|
||||
global := m.global
|
||||
platforms := m.platforms
|
||||
old := *m
|
||||
time.AfterFunc(time.Minute, func() {
|
||||
logger.Logger.Infof("mongo manager restart close...")
|
||||
global.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*Database); ok {
|
||||
v.Client.Disconnect(nil)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
platforms.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*sync.Map); ok {
|
||||
v.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*Database); ok {
|
||||
v.Client.Disconnect(nil)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return true
|
||||
})
|
||||
Close(&old)
|
||||
})
|
||||
|
||||
m.conf = conf
|
||||
|
@ -172,6 +153,28 @@ func (m *Manager) Restart(conf *Config) {
|
|||
m.platforms = &sync.Map{}
|
||||
}
|
||||
|
||||
func Close(m *Manager) {
|
||||
logger.Logger.Infof("mongo manager close")
|
||||
m.global.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*Database); ok {
|
||||
v.Client.Disconnect(nil)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
m.platforms.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*sync.Map); ok {
|
||||
v.Range(func(key, value any) bool {
|
||||
if v, ok := value.(*Database); ok {
|
||||
v.Client.Disconnect(nil)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (m *Manager) GetConfig() *Config {
|
||||
return m.conf
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue