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/sys/db"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
)
@ -18,6 +19,7 @@ DB组件也封装进来
type MCompModel struct {
cbase.ModuleCompBase
TableName string
Expired time.Duration //过期时间
Redis redis.ISys
DB mgo.ISys
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.DB = db.Local().Mgo
this.Redis = db.Local().Redis
this.Expired = time.Hour
return
}
func (this *MCompModel) Start() (err error) {
err = this.ModuleCompBase.Start()
if this.TableName == "" {
log.Panicf("TableName is nil")
return
}
this.DBModel = db.NewDBModel(this.TableName, db.Local())
this.DBModel = db.NewDBModel(this.TableName, this.Expired, db.Local())
return
}

View File

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

View File

@ -2,6 +2,7 @@ package gateway
import (
"go_dreamfactory/pb"
"sync"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
@ -31,3 +32,33 @@ type (
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/sys/log"
"go_dreamfactory/lego/utils/mapstructure"
"time"
)
type (
@ -38,34 +37,3 @@ func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
}
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) {
this.TableName = comm.TableShop
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Shop)
this.TableName = comm.TableShop
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
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) {
this.TableName = comm.TableShopitems
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Shop)
this.TableName = comm.TableShopitems
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
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 {
modules.MCompModel
moduleUser *User
module *User
}
func (this *ModelExpand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableUserExpand
err = this.MCompModel.Init(service, module, comp, options)
this.moduleUser = module.(*User)
this.module = module.(*User)
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) {
cuser = &pb.CacheUser{}
if err := this.Get(uid, cuser); err != nil {
this.moduleUser.Errorf("GetUserSession err:%v", err)
this.module.Errorf("GetUserSession err:%v", err)
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) {
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
}
err = nil
@ -46,6 +46,6 @@ func (this *ModelExpand) ChangeUserExpand(uid string, value map[string]interface
if len(value) == 0 {
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 (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"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) {
this.TableName = comm.TableSession
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User)
this.TableName = comm.TableSession
this.Expired = 0 //不自动过期
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) {
user = &pb.CacheUser{}
if err := this.GetListObj(comm.RDS_SESSION, uid, user); err != nil {
log.Errorf("getUserSession err:%v", err)
this.module.Errorln(err)
return nil
}
return user

View File

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

View File

@ -19,14 +19,14 @@ import (
type ModelUser struct {
modules.MCompModel
moduleUser *User
eventApp *event_v2.App
module *User
eventApp *event_v2.App
}
func (this *ModelUser) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableUser
err = this.MCompModel.Init(service, module, comp, options)
this.moduleUser = module.(*User)
this.module = module.(*User)
this.eventApp = event_v2.NewApp()
this.eventApp.Listen(comm.EventUserChanged, this.ChangeExp)
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) {
user = &pb.DBUser{}
if err := this.Get(uid, user); err != nil {
this.moduleUser.Errorf("getUser err:%v", err)
this.module.Errorln(err)
return
}
return
@ -87,7 +87,7 @@ func (this *ModelUser) updateUserAttr(uid string, data map[string]interface{}) e
func (this *ModelUser) isLoginFirst(timestamp int64) bool {
now := time.Now()
if timestamp == 0 || timestamp > now.Unix() {
this.moduleUser.Debugf("lastlogin time great now")
this.module.Debugf("lastlogin time great now")
return false
}
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) {
figureMap := make(map[int32]interface{})
for _, v := range this.moduleUser.configure.GetPlayerFigureConf() {
for _, v := range this.module.configure.GetPlayerFigureConf() {
figure := &pb.Figure{
Hair: &pb.Hair{Color: v.Figure[0].Color},
@ -148,12 +148,12 @@ func (this *ModelUser) InitFigure(uid string) {
update := map[string]interface{}{
"preinstall": figureMap,
}
this.moduleUser.modelExpand.ChangeUserExpand(uid, update)
this.module.modelExpand.ChangeUserExpand(uid, update)
}
func (this *ModelUser) updateOfflineTime(uid string) {
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{})) {
ul := event.(*UserListen)
curLv := ul.lv
curLvConf := this.moduleUser.configure.GetPlayerlvConf(curLv)
nextLvConf := this.moduleUser.configure.GetPlayerlvConf(curLv + 1)
curLvConf := this.module.configure.GetPlayerlvConf(curLv)
nextLvConf := this.module.configure.GetPlayerlvConf(curLv + 1)
if curLvConf.Exp == 0 || nextLvConf == nil { //最大等级
next(ul)
return
@ -181,8 +181,8 @@ func (this *ModelUser) ChangeLevel(event interface{}, next func(event interface{
update := map[string]interface{}{
"lv": ul.lv,
}
this.moduleUser.modelUser.Change(ul.session.GetUserId(), update)
ul.session.SendMsg(string(this.moduleUser.GetType()),
this.module.modelUser.Change(ul.session.GetUserId(), update)
ul.session.SendMsg(string(this.module.GetType()),
UserSubTypeLvChangedPush,
&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 接收用户登录通知
func (this *SCompGateRoute) NoticeUserLogin(ctx context.Context, args *pb.NoticeUserLoginReq, reply *pb.RPCMessageReply) error {
model := db.NewDBModel(comm.TableSession, db.Local())
model.AddList("online", args.UserId, map[string]interface{}{
model := db.NewDBModel(comm.TableSession, 0, db.Local())
model.AddList(comm.RDS_SESSION, args.UserId, map[string]interface{}{
"uid": args.UserId,
"sessionId": args.UserSessionId,
"serviceTag": args.ServiceTag,

View File

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

View File

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