This commit is contained in:
liwei1dao 2022-08-25 10:56:09 +08:00
parent 4c203aa3b2
commit e9cbc55192
13 changed files with 95 additions and 94 deletions

View File

@ -7,6 +7,7 @@ import (
"go_dreamfactory/lego/sys/redis" "go_dreamfactory/lego/sys/redis"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"log" "log"
"time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
@ -18,6 +19,7 @@ DB组件也封装进来
type MCompModel struct { type MCompModel struct {
cbase.ModuleCompBase cbase.ModuleCompBase
TableName string TableName string
Expired time.Duration //过期时间
Redis redis.ISys Redis redis.ISys
DB mgo.ISys DB mgo.ISys
DBModel *db.DBModel DBModel *db.DBModel
@ -28,11 +30,17 @@ func (this *MCompModel) Init(service core.IService, module core.IModule, comp co
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
this.DB = db.Local().Mgo this.DB = db.Local().Mgo
this.Redis = db.Local().Redis this.Redis = db.Local().Redis
this.Expired = time.Hour
return
}
func (this *MCompModel) Start() (err error) {
err = this.ModuleCompBase.Start()
if this.TableName == "" { if this.TableName == "" {
log.Panicf("TableName is nil") log.Panicf("TableName is nil")
return return
} }
this.DBModel = db.NewDBModel(this.TableName, db.Local()) this.DBModel = db.NewDBModel(this.TableName, this.Expired, db.Local())
return return
} }

View File

@ -220,23 +220,26 @@ func (this *Agent) Close() {
//分发用户消息 //分发用户消息
func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) { func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) {
var ( var (
req *pb.AgentMessage req *pb.AgentMessage = getmsg()
reply *pb.RPCMessageReply = &pb.RPCMessageReply{} reply *pb.RPCMessageReply = getmsgreply()
serviceTag string = "" serviceTag string = ""
servicePath string = comm.Service_Worker servicePath string = comm.Service_Worker
rule string rule string
ok bool ok bool
) )
req = &pb.AgentMessage{ defer func() {
Ip: this.IP(), putmsg(req)
UserSessionId: this.sessionId, putmsgreply(reply)
UserId: this.uId, }()
ServiceTag: this.gateway.Service().GetTag(), req.Ip = this.IP()
GatewayServiceId: this.gateway.Service().GetId(), req.UserSessionId = this.sessionId
MainType: msg.MainType, req.UserId = this.uId
SubType: msg.SubType, req.ServiceTag = this.gateway.Service().GetTag()
Message: msg.Data, req.GatewayServiceId = this.gateway.Service().GetId()
} req.MainType = msg.MainType
req.SubType = msg.SubType
req.Message = msg.Data
msgid := strings.ToLower(fmt.Sprintf("%s.%s", msg.MainType, msg.SubType)) msgid := strings.ToLower(fmt.Sprintf("%s.%s", msg.MainType, msg.SubType))
if rule, ok = this.gateway.GetMsgDistribute(msgid); ok { if rule, ok = this.gateway.GetMsgDistribute(msgid); ok {
paths := strings.Split(rule, "/") paths := strings.Split(rule, "/")

View File

@ -2,6 +2,7 @@ package gateway
import ( import (
"go_dreamfactory/pb" "go_dreamfactory/pb"
"sync"
"go_dreamfactory/lego/base" "go_dreamfactory/lego/base"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
@ -31,3 +32,33 @@ type (
GetMsgDistribute(msgid string) (rule string, ok bool) GetMsgDistribute(msgid string) (rule string, ok bool)
} }
) )
var msgPool = &sync.Pool{
New: func() interface{} {
return &pb.AgentMessage{}
},
}
func getmsg() *pb.AgentMessage {
req := msgPool.Get().(*pb.AgentMessage)
return req
}
func putmsg(r *pb.AgentMessage) {
msgPool.Put(r)
}
var msgreplyPool = &sync.Pool{
New: func() interface{} {
return &pb.RPCMessageReply{}
},
}
func getmsgreply() *pb.RPCMessageReply {
reply := msgreplyPool.Get().(*pb.RPCMessageReply)
return reply
}
func putmsgreply(r *pb.RPCMessageReply) {
msgreplyPool.Put(r)
}

View File

@ -5,7 +5,6 @@ import (
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure" "go_dreamfactory/lego/utils/mapstructure"
"time"
) )
type ( type (
@ -38,34 +37,3 @@ func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
} }
return return
} }
type DBOption func(*DBOptions)
type DBOptions struct {
IsMgoLog bool //是否写mgolog
Expire time.Duration //过期时间
}
//设置是否写mgor日志
func SetDBMgoLog(v bool) DBOption {
return func(o *DBOptions) {
o.IsMgoLog = v
}
}
//设置过期时间
func SetDBExpire(v time.Duration) DBOption {
return func(o *DBOptions) {
o.Expire = v
}
}
//更具 Option 序列化 系统参数对象
func newDBOption(opts ...DBOption) DBOptions {
options := DBOptions{
IsMgoLog: true,
}
for _, o := range opts {
o(&options)
}
return options
}

View File

@ -18,9 +18,9 @@ type modelShopComp struct {
//组件初始化接口 //组件初始化接口
func (this *modelShopComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { func (this *modelShopComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableShop
this.MCompModel.Init(service, module, comp, opt) this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Shop) this.module = module.(*Shop)
this.TableName = comm.TableShop
//创建uid索引 //创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},

View File

@ -19,9 +19,9 @@ type modelShopItemsComp struct {
//组件初始化接口 //组件初始化接口
func (this *modelShopItemsComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { func (this *modelShopItemsComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableShopitems
this.MCompModel.Init(service, module, comp, opt) this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Shop) this.module = module.(*Shop)
this.TableName = comm.TableShopitems
//创建uid索引 //创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}, {Key: "goodsid", Value: bsonx.Int32(1)}}, Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}, {Key: "goodsid", Value: bsonx.Int32(1)}},

View File

@ -11,13 +11,13 @@ import (
// 记录一些扩展数据 // 记录一些扩展数据
type ModelExpand struct { type ModelExpand struct {
modules.MCompModel modules.MCompModel
moduleUser *User module *User
} }
func (this *ModelExpand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *ModelExpand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableUserExpand this.TableName = comm.TableUserExpand
err = this.MCompModel.Init(service, module, comp, options) err = this.MCompModel.Init(service, module, comp, options)
this.moduleUser = module.(*User) this.module = module.(*User)
return return
} }
@ -25,7 +25,7 @@ func (this *ModelExpand) Init(service core.IService, module core.IModule, comp c
func (this *ModelExpand) getUserSession(uid string) (cuser *pb.CacheUser) { func (this *ModelExpand) getUserSession(uid string) (cuser *pb.CacheUser) {
cuser = &pb.CacheUser{} cuser = &pb.CacheUser{}
if err := this.Get(uid, cuser); err != nil { if err := this.Get(uid, cuser); err != nil {
this.moduleUser.Errorf("GetUserSession err:%v", err) this.module.Errorf("GetUserSession err:%v", err)
return return
} }
return return
@ -34,7 +34,7 @@ func (this *ModelExpand) getUserSession(uid string) (cuser *pb.CacheUser) {
//获取用户通过扩展表 //获取用户通过扩展表
func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) { func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) {
result = &pb.DBUserExpand{} result = &pb.DBUserExpand{}
if err = this.moduleUser.modelExpand.Get(uid, result); err != nil && redis.RedisNil != err { if err = this.module.modelExpand.Get(uid, result); err != nil && redis.RedisNil != err {
return return
} }
err = nil err = nil
@ -46,6 +46,6 @@ func (this *ModelExpand) ChangeUserExpand(uid string, value map[string]interface
if len(value) == 0 { if len(value) == 0 {
return nil return nil
} }
return this.moduleUser.modelExpand.Change(uid, value) return this.module.modelExpand.Change(uid, value)
} }

View File

@ -3,7 +3,6 @@ package user
import ( import (
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules" "go_dreamfactory/modules"
"go_dreamfactory/pb" "go_dreamfactory/pb"
) )
@ -14,9 +13,10 @@ type ModelSession struct {
} }
func (this *ModelSession) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *ModelSession) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableSession
err = this.MCompModel.Init(service, module, comp, options) err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User) this.module = module.(*User)
this.TableName = comm.TableSession
this.Expired = 0 //不自动过期
return return
} }
@ -24,7 +24,7 @@ func (this *ModelSession) Init(service core.IService, module core.IModule, comp
func (this *ModelSession) getUserSession(uid string) (user *pb.CacheUser) { func (this *ModelSession) getUserSession(uid string) (user *pb.CacheUser) {
user = &pb.CacheUser{} user = &pb.CacheUser{}
if err := this.GetListObj(comm.RDS_SESSION, uid, user); err != nil { if err := this.GetListObj(comm.RDS_SESSION, uid, user); err != nil {
log.Errorf("getUserSession err:%v", err) this.module.Errorln(err)
return nil return nil
} }
return user return user

View File

@ -15,13 +15,13 @@ import (
type ModelSetting struct { type ModelSetting struct {
modules.MCompModel modules.MCompModel
moduleUser *User module *User
} }
func (this *ModelSetting) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *ModelSetting) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableSetting this.TableName = comm.TableSetting
err = this.MCompModel.Init(service, module, comp, options) err = this.MCompModel.Init(service, module, comp, options)
this.moduleUser = module.(*User) this.module = module.(*User)
return return
} }
@ -41,14 +41,14 @@ func (this *ModelSetting) InitSetting(uid string) {
Xuanshang: true, Xuanshang: true,
} }
if err := this.Add(uid, setting); err != nil { if err := this.Add(uid, setting); err != nil {
this.moduleUser.Errorf("InitSetting err:%v", err) this.module.Errorf("InitSetting err:%v", err)
} }
} }
// 用户设置获取 // 用户设置获取
func (this *ModelSetting) GetSetting(uid string) *pb.DBUserSetting { func (this *ModelSetting) GetSetting(uid string) *pb.DBUserSetting {
setting := &pb.DBUserSetting{} setting := &pb.DBUserSetting{}
if err := this.moduleUser.modelSetting.Get(uid, setting); err != nil { if err := this.module.modelSetting.Get(uid, setting); err != nil {
return nil return nil
} }
return setting return setting
@ -59,12 +59,12 @@ func (this *ModelSetting) UpdateSetting(uid string, data map[string]interface{})
if len(data) == 0 { if len(data) == 0 {
return nil return nil
} }
return this.moduleUser.modelSetting.Change(uid, data) return this.module.modelSetting.Change(uid, data)
} }
//校验时间和初始次数 //校验时间和初始次数
func (this *ModelSetting) checkInitCount(uid string) bool { func (this *ModelSetting) checkInitCount(uid string) bool {
ue, err := this.moduleUser.modelExpand.GetUserExpand(uid) ue, err := this.module.modelExpand.GetUserExpand(uid)
if err != nil { if err != nil {
return false return false
} }
@ -93,12 +93,12 @@ func (this *ModelSetting) checkInitCount(uid string) bool {
func (this *ModelSetting) checkVeriCode(uid string) (int32, bool) { func (this *ModelSetting) checkVeriCode(uid string) (int32, bool) {
key := fmt.Sprintf("code:%s", uid) key := fmt.Sprintf("code:%s", uid)
var code int32 var code int32
err := this.moduleUser.modelSetting.Redis.Get(key, &code) err := this.module.modelSetting.Redis.Get(key, &code)
if err != nil { if err != nil {
if err == redis.RedisNil { if err == redis.RedisNil {
return 0, false return 0, false
} else { } else {
this.moduleUser.Errorf("%v", err) this.module.Errorf("%v", err)
} }
return 0, false return 0, false
} }
@ -114,8 +114,8 @@ func (this *ModelSetting) refresh(uid string) (code int32) {
return return
} else { } else {
code = cast.ToInt32(utils.GenValidateCode(6)) code = cast.ToInt32(utils.GenValidateCode(6))
if err := this.moduleUser.modelSetting.Redis.Set(key, code, time.Second*60); err != nil { if err := this.module.modelSetting.Redis.Set(key, code, time.Second*60); err != nil {
this.moduleUser.Errorf("%v", err) this.module.Errorf("%v", err)
return 0 return 0
} }
return return
@ -124,7 +124,7 @@ func (this *ModelSetting) refresh(uid string) (code int32) {
// 清空设置 // 清空设置
func (this *ModelSetting) cleanData(uid string) { func (this *ModelSetting) cleanData(uid string) {
if err := this.moduleUser.modelSetting.Del(uid); err != nil { if err := this.module.modelSetting.Del(uid); err != nil {
this.moduleUser.Errorf("cleanData err:%v", err) this.module.Errorf("cleanData err:%v", err)
} }
} }

View File

@ -19,14 +19,14 @@ import (
type ModelUser struct { type ModelUser struct {
modules.MCompModel modules.MCompModel
moduleUser *User module *User
eventApp *event_v2.App eventApp *event_v2.App
} }
func (this *ModelUser) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *ModelUser) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableUser this.TableName = comm.TableUser
err = this.MCompModel.Init(service, module, comp, options) err = this.MCompModel.Init(service, module, comp, options)
this.moduleUser = module.(*User) this.module = module.(*User)
this.eventApp = event_v2.NewApp() this.eventApp = event_v2.NewApp()
this.eventApp.Listen(comm.EventUserChanged, this.ChangeExp) this.eventApp.Listen(comm.EventUserChanged, this.ChangeExp)
this.eventApp.Listen(comm.EventUserChanged, this.ChangeLevel) this.eventApp.Listen(comm.EventUserChanged, this.ChangeLevel)
@ -72,7 +72,7 @@ func (this *ModelUser) User_Create(user *pb.DBUser) (err error) {
func (this *ModelUser) GetUser(uid string) (user *pb.DBUser) { func (this *ModelUser) GetUser(uid string) (user *pb.DBUser) {
user = &pb.DBUser{} user = &pb.DBUser{}
if err := this.Get(uid, user); err != nil { if err := this.Get(uid, user); err != nil {
this.moduleUser.Errorf("getUser err:%v", err) this.module.Errorln(err)
return return
} }
return return
@ -87,7 +87,7 @@ func (this *ModelUser) updateUserAttr(uid string, data map[string]interface{}) e
func (this *ModelUser) isLoginFirst(timestamp int64) bool { func (this *ModelUser) isLoginFirst(timestamp int64) bool {
now := time.Now() now := time.Now()
if timestamp == 0 || timestamp > now.Unix() { if timestamp == 0 || timestamp > now.Unix() {
this.moduleUser.Debugf("lastlogin time great now") this.module.Debugf("lastlogin time great now")
return false return false
} }
tt := time.Unix(timestamp, 0) tt := time.Unix(timestamp, 0)
@ -132,7 +132,7 @@ func (this *ModelUser) modifyName(uid string, newName string) (code pb.ErrorCode
// 初始化玩家形象 // 初始化玩家形象
func (this *ModelUser) InitFigure(uid string) { func (this *ModelUser) InitFigure(uid string) {
figureMap := make(map[int32]interface{}) figureMap := make(map[int32]interface{})
for _, v := range this.moduleUser.configure.GetPlayerFigureConf() { for _, v := range this.module.configure.GetPlayerFigureConf() {
figure := &pb.Figure{ figure := &pb.Figure{
Hair: &pb.Hair{Color: v.Figure[0].Color}, Hair: &pb.Hair{Color: v.Figure[0].Color},
@ -148,12 +148,12 @@ func (this *ModelUser) InitFigure(uid string) {
update := map[string]interface{}{ update := map[string]interface{}{
"preinstall": figureMap, "preinstall": figureMap,
} }
this.moduleUser.modelExpand.ChangeUserExpand(uid, update) this.module.modelExpand.ChangeUserExpand(uid, update)
} }
func (this *ModelUser) updateOfflineTime(uid string) { func (this *ModelUser) updateOfflineTime(uid string) {
if err := this.updateUserAttr(uid, map[string]interface{}{"offlinetime": time.Now().Unix()}); err != nil { if err := this.updateUserAttr(uid, map[string]interface{}{"offlinetime": time.Now().Unix()}); err != nil {
log.Errorf("updateOfflineTime err:%v", err) this.module.Errorln(err)
} }
} }
@ -168,8 +168,8 @@ func (this *ModelUser) ChangeExp(event interface{}, next func(event interface{})
func (this *ModelUser) ChangeLevel(event interface{}, next func(event interface{})) { func (this *ModelUser) ChangeLevel(event interface{}, next func(event interface{})) {
ul := event.(*UserListen) ul := event.(*UserListen)
curLv := ul.lv curLv := ul.lv
curLvConf := this.moduleUser.configure.GetPlayerlvConf(curLv) curLvConf := this.module.configure.GetPlayerlvConf(curLv)
nextLvConf := this.moduleUser.configure.GetPlayerlvConf(curLv + 1) nextLvConf := this.module.configure.GetPlayerlvConf(curLv + 1)
if curLvConf.Exp == 0 || nextLvConf == nil { //最大等级 if curLvConf.Exp == 0 || nextLvConf == nil { //最大等级
next(ul) next(ul)
return return
@ -181,8 +181,8 @@ func (this *ModelUser) ChangeLevel(event interface{}, next func(event interface{
update := map[string]interface{}{ update := map[string]interface{}{
"lv": ul.lv, "lv": ul.lv,
} }
this.moduleUser.modelUser.Change(ul.session.GetUserId(), update) this.module.modelUser.Change(ul.session.GetUserId(), update)
ul.session.SendMsg(string(this.moduleUser.GetType()), ul.session.SendMsg(string(this.module.GetType()),
UserSubTypeLvChangedPush, UserSubTypeLvChangedPush,
&pb.UserChangedPush{Uid: ul.session.GetUserId(), Exp: ul.exp, Lv: ul.lv}) &pb.UserChangedPush{Uid: ul.session.GetUserId(), Exp: ul.exp, Lv: ul.lv})
} }

View File

@ -185,8 +185,8 @@ func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessag
//RPC_NoticeUserClose 接收用户登录通知 //RPC_NoticeUserClose 接收用户登录通知
func (this *SCompGateRoute) NoticeUserLogin(ctx context.Context, args *pb.NoticeUserLoginReq, reply *pb.RPCMessageReply) error { func (this *SCompGateRoute) NoticeUserLogin(ctx context.Context, args *pb.NoticeUserLoginReq, reply *pb.RPCMessageReply) error {
model := db.NewDBModel(comm.TableSession, db.Local()) model := db.NewDBModel(comm.TableSession, 0, db.Local())
model.AddList("online", args.UserId, map[string]interface{}{ model.AddList(comm.RDS_SESSION, args.UserId, map[string]interface{}{
"uid": args.UserId, "uid": args.UserId,
"sessionId": args.UserSessionId, "sessionId": args.UserSessionId,
"serviceTag": args.ServiceTag, "serviceTag": args.ServiceTag,

View File

@ -70,10 +70,10 @@ type DBConn struct {
Mgo mgo.ISys Mgo mgo.ISys
} }
func NewDBModel(TableName string, conn *DBConn) *DBModel { func NewDBModel(tableName string, expired time.Duration, conn *DBConn) *DBModel {
return &DBModel{ return &DBModel{
TableName: TableName, TableName: tableName,
Expired: 2 * time.Minute, Expired: expired,
Redis: conn.Redis, Redis: conn.Redis,
DB: conn.Mgo, DB: conn.Mgo,
} }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure" "go_dreamfactory/lego/utils/mapstructure"
"time"
) )
//DB层配置 //DB层配置
@ -92,7 +91,6 @@ func newOptionsByOption(opts ...Option) (options *Options, err error) {
type DBOption func(*DBOptions) type DBOption func(*DBOptions)
type DBOptions struct { type DBOptions struct {
IsMgoLog bool //是否写mgolog IsMgoLog bool //是否写mgolog
Expire time.Duration //过期时间
} }
//设置是否写mgor日志 //设置是否写mgor日志
@ -102,13 +100,6 @@ func SetDBMgoLog(v bool) DBOption {
} }
} }
//设置过期时间
func SetDBExpire(v time.Duration) DBOption {
return func(o *DBOptions) {
o.Expire = v
}
}
//更具 Option 序列化 系统参数对象 //更具 Option 序列化 系统参数对象
func newDBOption(opts ...DBOption) DBOptions { func newDBOption(opts ...DBOption) DBOptions {
options := DBOptions{ options := DBOptions{