优化数据库连接对象错误保护
This commit is contained in:
parent
462c3d7dc0
commit
df28036e54
@ -28,8 +28,12 @@ type MCompModel struct {
|
||||
//组件初始化接口
|
||||
func (this *MCompModel) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.DB = db.Local().Mgo
|
||||
this.Redis = db.Local().Redis
|
||||
var conn *db.DBConn
|
||||
if conn, err = db.Local(); err != nil {
|
||||
return
|
||||
}
|
||||
this.DB = conn.Mgo
|
||||
this.Redis = conn.Redis
|
||||
this.Expired = time.Hour
|
||||
return
|
||||
}
|
||||
@ -40,7 +44,11 @@ func (this *MCompModel) Start() (err error) {
|
||||
log.Panicf("TableName is nil")
|
||||
return
|
||||
}
|
||||
this.DBModel = db.NewDBModel(this.TableName, this.Expired, db.Local())
|
||||
var conn *db.DBConn
|
||||
if conn, err = db.Local(); err != nil {
|
||||
return
|
||||
}
|
||||
this.DBModel = db.NewDBModel(this.TableName, this.Expired, conn)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -310,12 +310,20 @@ func (this *ModuleBase) DispenseRes(session comm.IUserSession, res []*cfg.Gameat
|
||||
}
|
||||
|
||||
//跨服对象获取数据操作对象
|
||||
func (this *ModuleBase) GetDBNodule(session comm.IUserSession, tableName string, expired time.Duration) *db.DBModel {
|
||||
func (this *ModuleBase) GetDBNodule(session comm.IUserSession, tableName string, expired time.Duration) (model *db.DBModel, err error) {
|
||||
var conn *db.DBConn
|
||||
if session.GetServiecTag() == this.service.GetTag() {
|
||||
return db.NewDBModel(tableName, expired, db.Local())
|
||||
if conn, err = db.Local(); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
} else {
|
||||
return db.NewDBModel(tableName, expired, db.ServerDBConn(session.GetServiecTag()))
|
||||
if conn, err = db.ServerDBConn(session.GetServiecTag()); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
model = db.NewDBModel(tableName, expired, conn)
|
||||
return
|
||||
}
|
||||
|
||||
//日志接口
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
cfg "go_dreamfactory/sys/configure/structs"
|
||||
"go_dreamfactory/sys/db"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
@ -140,9 +141,14 @@ func (this *modelDreamComp) trigger(session comm.IUserSession, source *pb.Battle
|
||||
///查询好友数据
|
||||
func (this *modelDreamComp) noticeuserfriend(session comm.IUserSession, mid string, chat *pb.DBChat) (code pb.ErrorCode) {
|
||||
var (
|
||||
err error
|
||||
model *db.DBModel
|
||||
err error
|
||||
)
|
||||
model := this.module.GetDBNodule(session, comm.TableFriend, 0)
|
||||
if model, err = this.module.GetDBNodule(session, comm.TableFriend, 0); err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
this.module.Errorf("session:%v err:%v", session, err)
|
||||
return
|
||||
}
|
||||
friend := &pb.DBFriend{Uid: session.GetUserId(), FriendIds: make([]string, 0)}
|
||||
if err = model.Get(session.GetUserId(), friend); err != nil && err != mgo.MongodbNil {
|
||||
this.module.Errorln(err)
|
||||
|
@ -182,7 +182,12 @@ func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessag
|
||||
|
||||
//RPC_NoticeUserClose 接收用户登录通知
|
||||
func (this *SCompGateRoute) NoticeUserLogin(ctx context.Context, args *pb.NoticeUserLoginReq, reply *pb.RPCMessageReply) error {
|
||||
model := db.NewDBModel(comm.TableSession, 0, db.Local())
|
||||
conn, err := db.Local()
|
||||
if err != nil {
|
||||
log.Errorf("[RPC] NoticeUserLogin err: %v", err)
|
||||
return err
|
||||
}
|
||||
model := db.NewDBModel(comm.TableSession, 0, conn)
|
||||
model.AddList(comm.RDS_SESSION, args.UserId, map[string]interface{}{
|
||||
"uid": args.UserId,
|
||||
"sessionId": args.UserSessionId,
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
type (
|
||||
ISys interface {
|
||||
//本服数据连接
|
||||
Local() *DBConn
|
||||
Local() (conn *DBConn, err error)
|
||||
//本服数据连接
|
||||
Cross() *DBConn
|
||||
Cross() (conn *DBConn, err error)
|
||||
//跨服列表数据层连接
|
||||
ServerDBConn(stage string) (conn *DBConn)
|
||||
ServerDBConn(stage string) (conn *DBConn, err error)
|
||||
///获取区服列表标签
|
||||
GetServerTags() []string
|
||||
//更新数据过期
|
||||
@ -47,15 +47,15 @@ func NewSys(option ...Option) (sys ISys, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Local() *DBConn {
|
||||
func Local() (conn *DBConn, err error) {
|
||||
return defsys.Local()
|
||||
}
|
||||
|
||||
func Cross() *DBConn {
|
||||
func Cross() (conn *DBConn, err error) {
|
||||
return defsys.Cross()
|
||||
}
|
||||
|
||||
func ServerDBConn(stage string) (conn *DBConn) {
|
||||
func ServerDBConn(stage string) (conn *DBConn, err error) {
|
||||
return defsys.ServerDBConn(stage)
|
||||
}
|
||||
|
||||
|
23
sys/db/db.go
23
sys/db/db.go
@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
@ -98,25 +99,27 @@ func (this *DB) readercrossconf(path string) (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
func (this *DB) Local() *DBConn {
|
||||
if this.local == nil {
|
||||
log.Panic("LocalDBConn on init")
|
||||
func (this *DB) Local() (conn *DBConn, err error) {
|
||||
conn = this.local
|
||||
if conn == nil {
|
||||
err = errors.New("LocalDBConn on init")
|
||||
}
|
||||
return this.local
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) Cross() *DBConn {
|
||||
if this.cross == nil {
|
||||
log.Panic("CrossDBConn on init")
|
||||
func (this *DB) Cross() (conn *DBConn, err error) {
|
||||
conn = this.cross
|
||||
if conn == nil {
|
||||
err = errors.New("CrossDBConn on init")
|
||||
}
|
||||
return this.cross
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) ServerDBConn(stage string) (conn *DBConn) {
|
||||
func (this *DB) ServerDBConn(stage string) (conn *DBConn, err error) {
|
||||
ok := false
|
||||
conn, ok = this.servers[stage]
|
||||
if !ok {
|
||||
log.Panicf("DBConn:%s on init", stage)
|
||||
err = fmt.Errorf("DBConn:%s on init", stage)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user