Module释放自动关闭注册事件

This commit is contained in:
duanhf2012
2020-04-20 18:14:41 +08:00
parent 742ffc410e
commit a906002f29
6 changed files with 81 additions and 59 deletions

View File

@@ -10,7 +10,7 @@ import (
"time"
)
const InitModuleId = 1e18
const InitModuleId = 1e17
type IModule interface {
@@ -53,7 +53,6 @@ type Module struct {
//事件管道
moduleName string
eventHandler event.EventHandler
//eventChan chan *SEvent
}
@@ -79,6 +78,10 @@ func (slf *Module) OnInit() error{
}
func (slf *Module) AddModule(module IModule) (int64,error){
//没有事件处理器不允许加入其他模块
if slf.GetEventProcessor() == nil {
return 0,fmt.Errorf("module %+v is not Event Processor is nil",slf.self)
}
pAddModule := module.getBaseModule().(*Module)
if pAddModule.GetModuleId()==0 {
pAddModule.moduleId = slf.NewModuleId()
@@ -111,13 +114,14 @@ func (slf *Module) AddModule(module IModule) (int64,error){
}
func (slf *Module) ReleaseModule(moduleId int64){
//pBaseModule := slf.GetModule(moduleId).getBaseModule().(*Module)
pModule := slf.GetModule(moduleId).getBaseModule().(*Module)
//释放子孙
for id,_ := range pModule.child {
slf.ReleaseModule(id)
}
pModule.GetEventHandler().Desctory()
pModule.self.OnRelease()
log.Debug("Release module %s.",slf.GetModuleName())
for pTimer,_ := range pModule.mapActiveTimer {

View File

@@ -39,7 +39,6 @@ type Service struct {
name string //service name
closeSig chan bool
wg sync.WaitGroup
this IService
serviceCfg interface{}
gorouterNum int32
startStatus bool
@@ -62,9 +61,9 @@ func (slf *Service) OpenProfiler() {
func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) {
slf.dispatcher =timer.NewDispatcher(timerDispatcherLen)
slf.this = iservice
slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun)
slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun)
slf.self = iservice.(IModule)
//初始化祖先
slf.ancestor = iservice.(IModule)
slf.seedModuleId =InitModuleId
@@ -72,7 +71,7 @@ func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getSer
slf.serviceCfg = serviceCfg
slf.gorouterNum = 1
slf.eventHandler.Init(&slf.eventProcessor)
slf.this.OnInit()
slf.self.OnInit()
}
func (slf *Service) SetGoRouterNum(gorouterNum int32) bool {
@@ -171,7 +170,7 @@ func (slf *Service) Release(){
log.Error("core dump info:%+v\n",err)
}
}()
slf.this.OnRelease()
slf.self.OnRelease()
log.Debug("Release Service %s.",slf.GetName())
}
@@ -182,7 +181,6 @@ func (slf *Service) OnInit() error {
return nil
}
func (slf *Service) Wait(){
slf.wg.Wait()
}
@@ -191,13 +189,10 @@ func (slf *Service) GetServiceCfg()interface{}{
return slf.serviceCfg
}
func (slf *Service) GetProfiler() *profiler.Profiler{
return slf.profiler
}
func (slf *Service) RegEventReciverFunc(eventType event.EventType,reciver event.IEventHandler,callback event.EventCallBack){
slf.eventProcessor.RegEventReciverFunc(eventType,reciver,callback)
}