Compare commits
No commits in common. "6a5eef06eaea34e3b1e0957ede8068fb93620324" and "5c38610b55efeaebfe3a633fd36f88321962127f" have entirely different histories.
6a5eef06ea
...
5c38610b55
|
@ -1,29 +0,0 @@
|
||||||
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,23 +9,8 @@ import (
|
||||||
newMongo "go.mongodb.org/mongo-driver/mongo"
|
newMongo "go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"mongo.games.com/goserver/core/logger"
|
"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
|
var globalMongoSession *newMongo.Client
|
||||||
|
|
||||||
func mongoURI(user, password, host string, port int32, options string) string {
|
func mongoURI(user, password, host string, port int32, options string) string {
|
||||||
|
@ -46,8 +31,6 @@ func mongoURI(user, password, host string, port int32, options string) string {
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMongoClient 创建mongo客户端
|
|
||||||
// Deprecated: use [mongo.games.com/game/mongo] instead
|
|
||||||
func NewMongoClient() (*newMongo.Client, error) {
|
func NewMongoClient() (*newMongo.Client, error) {
|
||||||
if globalMongoSession != nil {
|
if globalMongoSession != nil {
|
||||||
return globalMongoSession, nil
|
return globalMongoSession, nil
|
||||||
|
|
|
@ -69,10 +69,7 @@ func (svc *AnnouncerLogSvc) InsertAnnouncerLog(args []*model.AnnouncerLog, ret *
|
||||||
|
|
||||||
func (svc *AnnouncerLogSvc) FetchAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *[]model.AnnouncerLog) (err error) {
|
func (svc *AnnouncerLogSvc) FetchAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *[]model.AnnouncerLog) (err error) {
|
||||||
*ret, err = FetchAnnouncerLog(args.Plt)
|
*ret, err = FetchAnnouncerLog(args.Plt)
|
||||||
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
return
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *AwardLogSvc) UpsertAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *model.AnnouncerLog) error {
|
func (svc *AwardLogSvc) UpsertAnnouncerLog(args *model.FetchAnnouncerLogArgs, ret *model.AnnouncerLog) error {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package svc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/globalsign/mgo"
|
|
||||||
"github.com/globalsign/mgo/bson"
|
"github.com/globalsign/mgo/bson"
|
||||||
"mongo.games.com/game/dbproxy/mongo"
|
"mongo.games.com/game/dbproxy/mongo"
|
||||||
"mongo.games.com/game/model"
|
"mongo.games.com/game/model"
|
||||||
|
@ -35,10 +34,7 @@ type AwardLogSvc struct {
|
||||||
|
|
||||||
func (svc *AwardLogSvc) FetchAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) (err error) {
|
func (svc *AwardLogSvc) FetchAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) (err error) {
|
||||||
*ret, err = FetchAwardLog(args.Plt)
|
*ret, err = FetchAwardLog(args.Plt)
|
||||||
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
return
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *AwardLogSvc) UpsertAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) error {
|
func (svc *AwardLogSvc) UpsertAwardLog(args *model.FetchAwardLogArgs, ret *model.AwardLog) error {
|
||||||
|
|
|
@ -2,17 +2,13 @@ package mongo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"mongo.games.com/goserver/core/logger"
|
|
||||||
|
|
||||||
"mongo.games.com/game/mongo/internal"
|
"mongo.games.com/game/mongo/internal"
|
||||||
|
"mongo.games.com/goserver/core/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config = internal.Config
|
type Config = internal.Config
|
||||||
type DatabaseConfig = internal.DatabaseConfig
|
type DatabaseConfig = internal.DatabaseConfig
|
||||||
type Collection = internal.Collection
|
type Collection = internal.Collection
|
||||||
type Database = internal.Database
|
|
||||||
|
|
||||||
var _manager *internal.Manager
|
var _manager *internal.Manager
|
||||||
var _conf *internal.Config
|
var _conf *internal.Config
|
||||||
|
@ -23,9 +19,10 @@ func GetConfig() *Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init 初始化
|
// Init 初始化
|
||||||
func Init(conf *Config) {
|
func Init(conf *Config) error {
|
||||||
_conf = conf
|
_conf = conf
|
||||||
_manager = internal.NewManager(_conf)
|
_manager = internal.NewManager(_conf)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restart 重启
|
// Restart 重启
|
||||||
|
@ -37,11 +34,6 @@ func Restart() {
|
||||||
_manager.Restart(_conf)
|
_manager.Restart(_conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func Close() {
|
|
||||||
internal.Close(_manager)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetGlobalCollection 获取全局库
|
// GetGlobalCollection 获取全局库
|
||||||
// database: 数据库名称
|
// database: 数据库名称
|
||||||
// collection: 集合名称
|
// collection: 集合名称
|
||||||
|
@ -64,18 +56,3 @@ func GetCollection(platform, database, collection string) (*Collection, error) {
|
||||||
|
|
||||||
return _manager.GetCollection(platform, database, collection)
|
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,9 +143,28 @@ func (m *Manager) GetCollection(key, database, collection string) (*Collection,
|
||||||
|
|
||||||
func (m *Manager) Restart(conf *Config) {
|
func (m *Manager) Restart(conf *Config) {
|
||||||
logger.Logger.Infof("mongo manager restart...")
|
logger.Logger.Infof("mongo manager restart...")
|
||||||
old := *m
|
global := m.global
|
||||||
|
platforms := m.platforms
|
||||||
time.AfterFunc(time.Minute, func() {
|
time.AfterFunc(time.Minute, func() {
|
||||||
Close(&old)
|
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
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
m.conf = conf
|
m.conf = conf
|
||||||
|
@ -153,28 +172,6 @@ func (m *Manager) Restart(conf *Config) {
|
||||||
m.platforms = &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
|
|
||||||
})
|
|
||||||
|
|
||||||
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 {
|
func (m *Manager) GetConfig() *Config {
|
||||||
return m.conf
|
return m.conf
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue