This commit is contained in:
liwei1dao 2022-06-21 14:54:47 +08:00
commit 25257fcf9a
13 changed files with 65 additions and 47 deletions

View File

@ -34,7 +34,7 @@ func (r *Robot) handleFriendMsg(msg *pb.UserMessage) {
func (r *Robot) FriendList() {
req := &pb.Friend_List_Req{}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_List}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -55,7 +55,7 @@ func (r *Robot) FriendSearch(nickName string) {
NickName: nickName,
}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Search}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -76,7 +76,7 @@ func (r *Robot) FriendApply(friendId string) {
FriendId: friendId,
}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Apply}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -95,7 +95,7 @@ func (r *Robot) handleFriendApply(msg *pb.UserMessage) {
func (r *Robot) FriendApplyList() {
req := &pb.Friend_ApplyList_Req{}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_ApplyList}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -116,7 +116,7 @@ func (r *Robot) FriendAgree(friendIds []string) {
FriendIds: friendIds,
}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Agree}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -137,7 +137,7 @@ func (r *Robot) FriendRefuse(friendIds []string) {
FriendIds: friendIds,
}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Refuse}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -156,7 +156,7 @@ func (r *Robot) handleFriendRefuse(msg *pb.UserMessage) {
func (r *Robot) FriendBlacklist() {
req := &pb.Friend_BlackList_Req{}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Blacklist}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -175,7 +175,7 @@ func (r *Robot) handleFriendBlacklist(msg *pb.UserMessage) {
func (r *Robot) FriendAddBlack(friendId string) {
req := &pb.Friend_BlackAdd_Req{FriendId: friendId}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_AddBlack}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)
@ -196,7 +196,7 @@ func (r *Robot) FriendDelBlack(friendId string) {
FriendId: friendId,
}
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_DelBlack}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)

View File

@ -17,7 +17,7 @@ func (r *Robot) handlePackMsg(msg *pb.UserMessage) {
func (r *Robot) QueryUserPack() {
req := &pb.Pack_Getlist_Req{IType: 1}
head := &pb.UserMessage{MainType: "pack", SubType: "queryuserpackreq"}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)

View File

@ -45,7 +45,7 @@ func (r *Robot) CreateUser(NickName string) {
SubType: user.User_SubType_Create,
}
defer traceFunc(head.MainType, head.SubType, r.user.GetUId(), req)
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), req)
err := r.SendToClient(head, req)
if err != nil {
log.Fatal(err)

View File

@ -57,6 +57,10 @@ const (
Event_UserOffline core.Event_Key = "Event_UserOffline" //用户离线事件
)
const (
DBService_Status string = "DBService_status"
)
// 服务网关组件接口定义
type ISC_GateRouteComp interface {
core.IServiceComp

6
go.mod
View File

@ -21,9 +21,9 @@ require (
github.com/smallnest/rpcx v1.7.4
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
github.com/tidwall/gjson v1.14.1
github.com/ugorji/go/codec v1.2.7
github.com/valyala/fastrand v1.1.0
go.mongodb.org/mongo-driver v1.5.1
go.uber.org/zap v1.21.0
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3
@ -42,7 +42,6 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/edwingeng/doublejump v0.0.0-20210724020454-c82f1bcb3280 // indirect
@ -97,7 +96,6 @@ require (
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rpcxio/libkv v0.5.1-0.20210420120011-1fceaedca8a5 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/rubyist/circuitbreaker v2.2.1+incompatible // indirect
@ -112,7 +110,6 @@ require (
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
@ -134,5 +131,4 @@ require (
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

View File

@ -15,19 +15,26 @@ import (
type DB_Comp struct {
modules.Model_Comp
task chan string
_data *mongo.Cursor
task chan string
isInit bool
}
func (this *DB_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.Model_Comp.Init(service, module, comp, options)
this.task = make(chan string, TaskMaxNum)
this._data = new(mongo.Cursor)
return
}
func (this *DB_Comp) Start() (err error) {
err = this.Model_Comp.Start()
model_count := this.Model_TotalCount()
if model_count > 0 { //1000
this.Redis.Set(comm.DBService_Status, true, -1)
this.isInit = false
} else {
this.isInit = true
}
go this.run()
return
}
@ -40,6 +47,9 @@ func (this *DB_Comp) run() {
case <-time.After(time.Second * 2):
this.Model_UpdateDBByLog("")
}
if !this.isInit && this.Model_TotalCount() <= 0 {
this.Redis.Delete(comm.DBService_Status)
}
}
}
@ -48,24 +58,24 @@ func (this *DB_Comp) PushUserTask(uid string) {
}
func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) {
var _data *mongo.Cursor
if uid == "" {
this._data, err = this.DB.Find(DB_ModelTable, bson.M{}, options.Find().SetLimit(int64(WriteMaxNum)))
_data, err = this.DB.Find(DB_ModelTable, bson.M{}, options.Find().SetLimit(int64(WriteMaxNum)))
if err != nil {
return err
}
} else {
this._data, err = this.DB.Find(DB_ModelTable, bson.M{"uid": uid}, options.Find())
_data, err = this.DB.Find(DB_ModelTable, bson.M{"uid": uid}, options.Find())
if err != nil {
return err
}
}
_delID := make([]string, 0) // 处理完成要删除的id
for this._data.Next(context.TODO()) { // 处理删除逻辑
_delID := make([]string, 0) // 处理完成要删除的id
for _data.Next(context.TODO()) { // 处理删除逻辑
data := &comm.Autogenerated{}
if err = this._data.Decode(data); err != nil {
if err = _data.Decode(data); err != nil {
log.Errorf("Decode Data err : %v", err)
continue
}
@ -178,7 +188,7 @@ func (this *DB_Comp) Model_InsertDBByLog(data *comm.Autogenerated) (err error) {
// 查询 当前日志列表还有没有处理完条数
func (this *DB_Comp) Model_TotalCount() int {
_data, err := this.DB.Find("model_log", bson.M{})
_data, err := this.DB.Find("DB_ModelTable", bson.M{})
if err == nil {
return _data.RemainingBatchLength()
}

View File

@ -31,7 +31,7 @@ func (this *ApiComp) Search(session comm.IUserSession, chk map[string]interface{
user := this.module.model_friend.Frined_FindCond(req.NickName)
if user != nil {
friend = &pb.FriendBase{
UserId: user.UId,
UserId: user.Uid,
NickName: user.Name,
}
}

View File

@ -5,6 +5,9 @@ import (
"go_dreamfactory/modules"
"go_dreamfactory/lego/core"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
const (
@ -26,6 +29,11 @@ func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core
this.MComp_GateComp.Init(service, module, comp, options)
this.service = service
this.module = module.(*Mail)
//创建uid索引
this.module.db_comp.DB.CreateIndex(core.SqlTable(DB_MailTable), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}

View File

@ -33,7 +33,7 @@ func (this *Api_Comp) Login(session comm.IUserSession, result map[string]interfa
code = pb.ErrorCode_SystemError
return
}
event.TriggerEvent(comm.Event_UserLogin, user.UId)
event.TriggerEvent(comm.Event_UserLogin, user.Uid)
}
}()
@ -57,7 +57,7 @@ func (this *Api_Comp) Login(session comm.IUserSession, result map[string]interfa
}
//bind user
err = session.Bind(user.UId, this.service.GetId())
err = session.Bind(user.Uid, this.service.GetId())
if err != nil {
code = pb.ErrorCode_BindUser
return
@ -69,8 +69,8 @@ func (this *Api_Comp) Login(session comm.IUserSession, result map[string]interfa
user.Createip = session.GetIP()
//缓存user session
err = this.module.modelSession.Change(user.UId, map[string]interface{}{
"uid": user.UId,
err = this.module.modelSession.Change(user.Uid, map[string]interface{}{
"uid": user.Uid,
"sessionId": session.GetSessionId(),
"gatewayServiceId": session.GetGatewayServiceId(),
},
@ -82,7 +82,7 @@ func (this *Api_Comp) Login(session comm.IUserSession, result map[string]interfa
}
//缓存user
err = this.module.modelUser.Add(user.UId, user, cache.WithDisabledMgoLog())
err = this.module.modelUser.Add(user.Uid, user, cache.WithDisabledMgoLog())
if err != nil {
code = pb.ErrorCode_DBError
return

View File

@ -51,7 +51,7 @@ func (this *ModelUser) User_FindById(id string) (*pb.DB_UserData, error) {
func (this *ModelUser) User_Create(user *pb.DB_UserData) (err error) {
_id := primitive.NewObjectID().Hex()
user.Id = _id
user.UId = fmt.Sprintf("%d_%s", user.Sid, _id)
user.Uid = fmt.Sprintf("%d_%s", user.Sid, _id)
user.Uuid = uuid.NewV4().String()
user.Ctime = time.Now().Unix()
_, err = this.DB.InsertOne(DB_UserTable, user)
@ -62,7 +62,7 @@ func (this *ModelUser) User_Create(user *pb.DB_UserData) (err error) {
func (this *ModelUser) User_Update(data *pb.DB_UserData) (err error) {
err = this.DB.FindOneAndUpdate(
DB_UserTable,
bson.M{"uid": data.UId},
bson.M{"uid": data.Uid},
bson.M{"$set": bson.M{
"name": data.Name,
}},

View File

@ -38,7 +38,7 @@ func (this *Api_Comp) Register(c *engine.Context) {
err := c.BindJSON(&req)
if err == nil {
err := this.module.modelUser.User_Create(&pb.DB_UserData{
BinduId: req.Account,
Binduid: req.Account,
Sid: req.Sid,
})
if err != nil {

View File

@ -10,9 +10,9 @@ message Cache_UserData {
message DB_UserData {
string id = 1; //@go_tags(`bson:"_id"`) ID
string uId = 2; //@go_tags(`bson:"uId"`) ID
string uid = 2; //@go_tags(`bson:"uid"`) ID
string uuid = 3; //@go_tags(`bson:"uuid"`) uuid
string binduId = 4; //@go_tags(`bson:"binduId"`)
string binduid = 4; //@go_tags(`bson:"binduid"`)
string name = 5; //@go_tags(`bson:"name"`)
int32 sid = 6; //@go_tags(`bson:"sid"`) id
string createip = 7; //@go_tags(`bson:"createip"`) ip

View File

@ -89,9 +89,9 @@ type DB_UserData struct {
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID
UId string `protobuf:"bytes,2,opt,name=uId,proto3" json:"uId" bson:"uId"` //用户ID
Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid" bson:"uid"` //用户ID
Uuid string `protobuf:"bytes,3,opt,name=uuid,proto3" json:"uuid" bson:"uuid"` //玩家唯一uuid
BinduId string `protobuf:"bytes,4,opt,name=binduId,proto3" json:"binduId" bson:"binduId"` //玩家账号
Binduid string `protobuf:"bytes,4,opt,name=binduid,proto3" json:"binduid" bson:"binduid"` //玩家账号
Name string `protobuf:"bytes,5,opt,name=name,proto3" json:"name" bson:"name"` //玩家名
Sid int32 `protobuf:"varint,6,opt,name=sid,proto3" json:"sid" bson:"sid"` //区服id
Createip string `protobuf:"bytes,7,opt,name=createip,proto3" json:"createip" bson:"createip"` //创建账号时的ip
@ -141,9 +141,9 @@ func (x *DB_UserData) GetId() string {
return ""
}
func (x *DB_UserData) GetUId() string {
func (x *DB_UserData) GetUid() string {
if x != nil {
return x.UId
return x.Uid
}
return ""
}
@ -155,9 +155,9 @@ func (x *DB_UserData) GetUuid() string {
return ""
}
func (x *DB_UserData) GetBinduId() string {
func (x *DB_UserData) GetBinduid() string {
if x != nil {
return x.BinduId
return x.Binduid
}
return ""
}
@ -231,11 +231,11 @@ var file_user_user_db_proto_rawDesc = []byte{
0x52, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
0x49, 0x64, 0x22, 0xaf, 0x02, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61,
0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x03, 0x75, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01,
0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01,
0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x64,
0x75, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75,
0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09,
0x75, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75,
0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09,
0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x06, 0x20,
0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x69, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x72, 0x65, 0x61,
0x74, 0x65, 0x69, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x72, 0x65, 0x61,