新增服务间事件通知机制

This commit is contained in:
duanhf2012
2020-04-20 15:38:42 +08:00
parent 990fec396a
commit 742ffc410e
10 changed files with 311 additions and 123 deletions

View File

@@ -26,8 +26,9 @@ type IModule interface {
OnRelease()
getBaseModule() IModule
GetService() IService
GetEventChan() chan *event.Event
GetModuleName() string
GetEventProcessor()event.IEventProcessor
NotifyEvent(ev *event.Event)
}
@@ -50,8 +51,8 @@ type Module struct {
descendants map[int64]IModule//始祖的后裔们
//事件管道
event.EventProcessor
moduleName string
eventHandler event.EventHandler
//eventChan chan *SEvent
}
@@ -96,6 +97,7 @@ func (slf *Module) AddModule(module IModule) (int64,error){
pAddModule.dispatcher = slf.GetAncestor().getBaseModule().(*Module).dispatcher
pAddModule.ancestor = slf.ancestor
pAddModule.moduleName = reflect.Indirect(reflect.ValueOf(module)).Type().Name()
pAddModule.eventHandler.Init(slf.eventHandler.GetEventProcessor())
err := module.OnInit()
if err != nil {
return 0,err
@@ -202,3 +204,14 @@ func (slf *Module) GetService() IService {
return slf.GetAncestor().(IService)
}
func (slf *Module) GetEventProcessor() event.IEventProcessor{
return slf.eventHandler.GetEventProcessor()
}
func (slf *Module) NotifyEvent(ev *event.Event){
slf.eventHandler.NotifyEvent(ev)
}
func (slf *Module) GetEventHandler() event.IEventHandler{
return &slf.eventHandler
}

View File

@@ -2,6 +2,7 @@ package service
import (
"fmt"
"github.com/duanhf2012/origin/event"
"github.com/duanhf2012/origin/log"
"github.com/duanhf2012/origin/profiler"
"github.com/duanhf2012/origin/rpc"
@@ -42,7 +43,7 @@ type Service struct {
serviceCfg interface{}
gorouterNum int32
startStatus bool
eventProcessor event.EventProcessor //事件接收者
profiler *profiler.Profiler //性能分析器
}
@@ -70,6 +71,7 @@ func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getSer
slf.descendants = map[int64]IModule{}
slf.serviceCfg = serviceCfg
slf.gorouterNum = 1
slf.eventHandler.Init(&slf.eventProcessor)
slf.this.OnInit()
}
@@ -101,7 +103,7 @@ func (slf *Service) Run() {
for{
rpcRequestChan := slf.GetRpcRequestChan()
rpcResponeCallBack := slf.GetRpcResponeChan()
eventChan := slf.GetEventChan()
eventChan := slf.eventProcessor.GetEventChan()
var analyzer *profiler.Analyzer
select {
case <- closeSig:
@@ -129,7 +131,7 @@ func (slf *Service) Run() {
if slf.profiler!=nil {
analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
}
slf.EventHandler(ev)
slf.eventProcessor.EventHandler(ev)
if analyzer!=nil {
analyzer.Pop()
analyzer = nil
@@ -195,3 +197,11 @@ func (slf *Service) GetProfiler() *profiler.Profiler{
}
func (slf *Service) RegEventReciverFunc(eventType event.EventType,reciver event.IEventHandler,callback event.EventCallBack){
slf.eventProcessor.RegEventReciverFunc(eventType,reciver,callback)
}
func (slf *Service) UnRegEventReciverFun(eventType event.EventType,reciver event.IEventHandler){
slf.eventProcessor.UnRegEventReciverFun(eventType,reciver)
}