Compare commits
3 Commits
5c38610b55
...
6a5eef06ea
Author | SHA1 | Date |
---|---|---|
|
6a5eef06ea | |
|
8210803b99 | |
|
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
|
||||
|
|
|
@ -69,7 +69,10 @@ func (svc *AnnouncerLogSvc) InsertAnnouncerLog(args []*model.AnnouncerLog, ret *
|
|||
|
||||
func (svc *AnnouncerLogSvc) FetchAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *[]model.AnnouncerLog) (err error) {
|
||||
*ret, err = FetchAnnouncerLog(args.Plt)
|
||||
return
|
||||
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svc *AwardLogSvc) UpsertAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *model.AnnouncerLog) error {
|
||||
|
|
|
@ -2,6 +2,7 @@ package svc
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/globalsign/mgo"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
"mongo.games.com/game/dbproxy/mongo"
|
||||
"mongo.games.com/game/model"
|
||||
|
@ -34,7 +35,10 @@ type AwardLogSvc struct {
|
|||
|
||||
func (svc *AwardLogSvc) FetchAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) (err error) {
|
||||
*ret, err = FetchAwardLog(args.Plt)
|
||||
return
|
||||
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svc *AwardLogSvc) UpsertAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) error {
|
||||
|
|
|
@ -2,13 +2,17 @@ package mongo
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"mongo.games.com/game/mongo/internal"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"mongo.games.com/goserver/core/logger"
|
||||
|
||||
"mongo.games.com/game/mongo/internal"
|
||||
)
|
||||
|
||||
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 +23,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 +37,11 @@ func Restart() {
|
|||
_manager.Restart(_conf)
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func Close() {
|
||||
internal.Close(_manager)
|
||||
}
|
||||
|
||||
// GetGlobalCollection 获取全局库
|
||||
// database: 数据库名称
|
||||
// collection: 集合名称
|
||||
|
@ -56,3 +64,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,18 +143,26 @@ 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 {
|
||||
Close(&old)
|
||||
})
|
||||
|
||||
m.conf = conf
|
||||
m.global = &sync.Map{}
|
||||
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
|
||||
})
|
||||
|
||||
platforms.Range(func(key, value any) bool {
|
||||
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 {
|
||||
|
@ -165,11 +173,6 @@ func (m *Manager) Restart(conf *Config) {
|
|||
}
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
m.conf = conf
|
||||
m.global = &sync.Map{}
|
||||
m.platforms = &sync.Map{}
|
||||
}
|
||||
|
||||
func (m *Manager) GetConfig() *Config {
|
||||
|
|
Loading…
Reference in New Issue