From 25cc58223c1d7bde389467c5d34b0c8789d74754 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Wed, 15 Jun 2022 16:23:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/dbservice/api.go | 25 --------------- modules/dbservice/configure_comp.go | 16 ---------- modules/dbservice/core.go | 26 ++++++++++++++++ modules/dbservice/db_comp.go | 48 ++++++++++++++--------------- modules/dbservice/dbservice_comp.go | 40 ------------------------ modules/dbservice/mail_test.go | 25 ++++++++------- modules/dbservice/module.go | 8 +---- modules/pack/api.go | 18 ++++++++++- 8 files changed, 80 insertions(+), 126 deletions(-) delete mode 100644 modules/dbservice/api.go delete mode 100644 modules/dbservice/configure_comp.go create mode 100644 modules/dbservice/core.go delete mode 100644 modules/dbservice/dbservice_comp.go diff --git a/modules/dbservice/api.go b/modules/dbservice/api.go deleted file mode 100644 index b0f57528b..000000000 --- a/modules/dbservice/api.go +++ /dev/null @@ -1,25 +0,0 @@ -package dbservice - -import ( - "go_dreamfactory/modules" - - "go_dreamfactory/lego/core" -) - -type Api_Comp struct { - modules.MComp_GateComp - service core.IService - module *DBService -} - -func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { - this.MComp_GateComp.Init(service, module, comp, options) - this.service = service - this.module = module.(*DBService) - return -} - -func (this *Api_Comp) Start() (err error) { - - return -} diff --git a/modules/dbservice/configure_comp.go b/modules/dbservice/configure_comp.go deleted file mode 100644 index 8a01baa1b..000000000 --- a/modules/dbservice/configure_comp.go +++ /dev/null @@ -1,16 +0,0 @@ -package dbservice - -import ( - "go_dreamfactory/lego/core" - "go_dreamfactory/lego/core/cbase" -) - -// 邮件配置管理组件 -type Configure_Comp struct { - cbase.ModuleCompBase -} - -func (this *Configure_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { - this.ModuleCompBase.Init(service, module, comp, options) - return -} diff --git a/modules/dbservice/core.go b/modules/dbservice/core.go new file mode 100644 index 000000000..f4b9d8432 --- /dev/null +++ b/modules/dbservice/core.go @@ -0,0 +1,26 @@ +package dbservice + +import ( + "go_dreamfactory/lego/core" + + "go.mongodb.org/mongo-driver/bson" +) + +const ( + WriteMaxNum uint32 = 1000 //一次性最处理条数 + ErrorMaxNum uint32 = 5 // 数据库操作最大失败次数 + TaskMaxNum uint32 = 1000 // 玩家离线消息队列长度 +) + +var ( + ErrorLogCount = make(map[string]uint32, 0) +) + +type QueryStruct struct { + Selector bson.M + Query bson.M +} + +const ( + DB_ModelTable core.SqlTable = "model_log" +) diff --git a/modules/dbservice/db_comp.go b/modules/dbservice/db_comp.go index 077a6e09f..228970b60 100644 --- a/modules/dbservice/db_comp.go +++ b/modules/dbservice/db_comp.go @@ -6,43 +6,43 @@ import ( "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" + "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) -const ( - WriteMaxNum uint32 = 1000 //一次性最处理条数 - ErrorMaxNum uint32 = 5 // 数据库操作最大失败次数 -) - -var ( - ErrorLogCount = make(map[string]uint32, 0) -) - type DB_Comp struct { modules.Model_Comp + task chan string } -// type data struct { -// Table string -// Wheremap map[string]interface{} // 如果是insert 条件就是nil del 只有条件 -// Modifymap map[string]map[string]interface{} -// } - -type QueryStruct struct { - Selector bson.M - Query bson.M +func (this *DB_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.ModuleCompBase.Init(service, module, comp, options) + this.task = make(chan string, TaskMaxNum) + return } -const ( - DB_ModelTable core.SqlTable = "model_log" -) +func (this *DB_Comp) Start() (err error) { + err = this.ModuleCompBase.Start() + go this.run() + return +} -type IModel interface { - Model_UpdateDBByLog() (err error) // 读取日志并更新对应的表 - Model_InsertDBByLog(data *comm.Autogenerated) (err error) // 插入日志 +func (this *DB_Comp) run() { + for { + select { + case v := <-this.task: + this.Model_UpdateDBByLog(v) + case <-time.After(time.Second * 2): + this.Model_UpdateDBByLog("") + } + } +} + +func (this *DB_Comp) PushUserTask(uid string) { + this.task <- uid } func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) { diff --git a/modules/dbservice/dbservice_comp.go b/modules/dbservice/dbservice_comp.go deleted file mode 100644 index cbe688147..000000000 --- a/modules/dbservice/dbservice_comp.go +++ /dev/null @@ -1,40 +0,0 @@ -package dbservice - -import ( - "go_dreamfactory/lego/core" - "go_dreamfactory/lego/core/cbase" - "time" -) - -type DBService_Comp struct { - cbase.ModuleCompBase - task chan string - module *DBService -} - -func (this *DBService_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { - this.ModuleCompBase.Init(service, module, comp, options) - this.module = module.(*DBService) - return -} - -func (this *DBService_Comp) Start() (err error) { - err = this.ModuleCompBase.Start() - go this.run() - return -} - -func (this *DBService_Comp) run() { - for { - select { - case v := <-this.task: - this.module.db_comp.Model_UpdateDBByLog(v) - case <-time.After(time.Second * 2): - this.module.db_comp.Model_UpdateDBByLog("") - } - } -} - -func (this *DBService_Comp) PushUserTask(uid string) { - this.task <- uid -} diff --git a/modules/dbservice/mail_test.go b/modules/dbservice/mail_test.go index c8dc3b648..7a7377708 100644 --- a/modules/dbservice/mail_test.go +++ b/modules/dbservice/mail_test.go @@ -1,8 +1,6 @@ package dbservice import ( - "go_dreamfactory/comm" - "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" "os" "testing" @@ -27,20 +25,21 @@ func TestMain(m *testing.M) { Check: false, Reward: false, } + module.db_comp.InsertModelLogs("mail", "uid123", _mail) //db.InsertModelLogs("mail", "uid123", _mail) //InsertModelLogs("mail", "uid123", _mail) - data := &comm.Autogenerated{ - ID: primitive.NewObjectID().Hex(), - UID: "uid123", - Act: string(comm.LogHandleType_Insert), - } - data.D = append(data.D, "mail") // D[0] - data.D = append(data.D, _mail) // D[1] + // data := &comm.Autogenerated{ + // ID: primitive.NewObjectID().Hex(), + // UID: "uid123", + // Act: string(comm.LogHandleType_Insert), + // } + // data.D = append(data.D, "mail") // D[0] + // data.D = append(data.D, _mail) // D[1] - _, err1 := module.db_comp.DB.InsertOne("model_log", data) - if err1 != nil { - log.Errorf("insert model db err %v", err1) - } + // _, err1 := module.db_comp.DB.InsertOne("model_log", data) + // if err1 != nil { + // log.Errorf("insert model db err %v", err1) + // } //}() } time.Sleep(time.Second * 10) diff --git a/modules/dbservice/module.go b/modules/dbservice/module.go index 7988738b1..14ee46e59 100644 --- a/modules/dbservice/module.go +++ b/modules/dbservice/module.go @@ -13,10 +13,7 @@ func NewModule() core.IModule { type DBService struct { modules.ModuleBase - api_comp *Api_Comp - db_comp *DB_Comp - db_service *DBService_Comp - configure_comp *Configure_Comp + db_comp *DB_Comp } func (this *DBService) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { @@ -31,8 +28,5 @@ func (this *DBService) GetType() core.M_Modules { func (this *DBService) OnInstallComp() { this.ModuleBase.OnInstallComp() - this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) - this.db_service = this.RegisterComp(new(DBService_Comp)).(*DBService_Comp) - this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } diff --git a/modules/pack/api.go b/modules/pack/api.go index ce2ba47fc..1324b3719 100644 --- a/modules/pack/api.go +++ b/modules/pack/api.go @@ -1,6 +1,7 @@ package pack import ( + "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/lego/core" @@ -17,12 +18,27 @@ const ( //消息回复的头名称 */ type Api_Comp struct { modules.MComp_GateComp - module *Pack + service core.IService + module *Pack + mail comm.Imail } //组件初始化接口 func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.MComp_GateComp.Init(service, module, comp, options) this.module = module.(*Pack) + + return +} + +func (this *Api_Comp) Start() (err error) { + err = this.MComp_GateComp.Start() + var module core.IModule + + if module, err = this.service.GetModule(comm.SM_PackModule); err != nil { + return + } + this.mail = module.(comm.Imail) + return }