优化工程结构

This commit is contained in:
boyce
2020-10-30 16:32:37 +08:00
parent 3025eaebd5
commit d2f52b382d
33 changed files with 1087 additions and 1210 deletions

View File

@@ -19,10 +19,10 @@ var closeSig chan bool
var timerDispatcherLen = 10000
type IService interface {
Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{})
Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{})
SetName(serviceName string)
GetName() string
OnSetup(iservice IService)
OnSetup(iService IService)
OnInit() error
OnRelease()
Wait()
@@ -33,115 +33,112 @@ type IService interface {
GetProfiler() *profiler.Profiler
}
type Service struct {
Module
rpc.RpcHandler //rpc
name string //service name
wg sync.WaitGroup
serviceCfg interface{}
gorouterNum int32
startStatus bool
rpc.RpcHandler //rpc
name string //service name
wg sync.WaitGroup
serviceCfg interface{}
goroutineNum int32
startStatus bool
eventProcessor event.IEventProcessor
//eventProcessor event.EventProcessor //事件接收者
profiler *profiler.Profiler //性能分析器
}
func (slf *Service) OnSetup(iservice IService){
if iservice.GetName() == "" {
slf.name = reflect.Indirect(reflect.ValueOf(iservice)).Type().Name()
func (s *Service) OnSetup(iService IService){
if iService.GetName() == "" {
s.name = reflect.Indirect(reflect.ValueOf(iService)).Type().Name()
}
}
func (slf *Service) OpenProfiler() {
slf.profiler = profiler.RegProfiler(slf.GetName())
if slf.profiler==nil {
log.Fatal("rofiler.RegProfiler %s fail.",slf.GetName())
func (s *Service) OpenProfiler() {
s.profiler = profiler.RegProfiler(s.GetName())
if s.profiler==nil {
log.Fatal("rofiler.RegProfiler %s fail.", s.GetName())
}
}
func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) {
slf.dispatcher =timer.NewDispatcher(timerDispatcherLen)
func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) {
s.dispatcher =timer.NewDispatcher(timerDispatcherLen)
slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun)
slf.self = iservice.(IModule)
s.InitRpcHandler(iService.(rpc.IRpcHandler),getClientFun,getServerFun)
s.self = iService.(IModule)
//初始化祖先
slf.ancestor = iservice.(IModule)
slf.seedModuleId =InitModuleId
slf.descendants = map[int64]IModule{}
slf.serviceCfg = serviceCfg
slf.gorouterNum = 1
slf.eventProcessor = event.NewEventProcessor()
slf.eventHandler = event.NewEventHandler()
slf.eventHandler.Init(slf.eventProcessor)
s.ancestor = iService.(IModule)
s.seedModuleId =InitModuleId
s.descendants = map[int64]IModule{}
s.serviceCfg = serviceCfg
s.goroutineNum = 1
s.eventProcessor = event.NewEventProcessor()
s.eventHandler = event.NewEventHandler()
s.eventHandler.Init(s.eventProcessor)
}
func (slf *Service) SetGoRouterNum(gorouterNum int32) bool {
func (s *Service) SetGoRouterNum(goroutineNum int32) bool {
//已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程
if slf.startStatus == true || slf.profiler!=nil {
if s.startStatus == true || s.profiler!=nil {
log.Error("open profiler mode is not allowed to set Multi-coroutine.")
return false
}
slf.gorouterNum = gorouterNum
s.goroutineNum = goroutineNum
return true
}
func (slf *Service) Start() {
slf.startStatus = true
for i:=int32(0);i<slf.gorouterNum;i++{
slf.wg.Add(1)
func (s *Service) Start() {
s.startStatus = true
for i:=int32(0);i< s.goroutineNum;i++{
s.wg.Add(1)
go func(){
slf.Run()
s.Run()
}()
}
}
func (slf *Service) Run() {
log.Debug("Start running Service %s.",slf.GetName())
defer slf.wg.Done()
func (s *Service) Run() {
log.Debug("Start running Service %s.", s.GetName())
defer s.wg.Done()
var bStop = false
for{
rpcRequestChan := slf.GetRpcRequestChan()
rpcResponeCallBack := slf.GetRpcResponeChan()
eventChan := slf.eventProcessor.GetEventChan()
rpcRequestChan := s.GetRpcRequestChan()
rpcResponseCallBack := s.GetRpcResponseChan()
eventChan := s.eventProcessor.GetEventChan()
var analyzer *profiler.Analyzer
select {
case <- closeSig:
bStop = true
case rpcRequest :=<- rpcRequestChan:
if slf.profiler!=nil {
analyzer = slf.profiler.Push("Req_"+rpcRequest.RpcRequestData.GetServiceMethod())
if s.profiler!=nil {
analyzer = s.profiler.Push("Req_"+rpcRequest.RpcRequestData.GetServiceMethod())
}
slf.GetRpcHandler().HandlerRpcRequest(rpcRequest)
s.GetRpcHandler().HandlerRpcRequest(rpcRequest)
if analyzer!=nil {
analyzer.Pop()
analyzer = nil
}
case rpcResponeCB := <- rpcResponeCallBack:
if slf.profiler!=nil {
analyzer = slf.profiler.Push("Res_" + rpcResponeCB.ServiceMethod)
case rpcResponseCB := <-rpcResponseCallBack:
if s.profiler!=nil {
analyzer = s.profiler.Push("Res_" + rpcResponseCB.ServiceMethod)
}
slf.GetRpcHandler().HandlerRpcResponeCB(rpcResponeCB)
s.GetRpcHandler().HandlerRpcResponseCB(rpcResponseCB)
if analyzer!=nil {
analyzer.Pop()
analyzer = nil
}
case ev := <- eventChan:
if slf.profiler!=nil {
analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
if s.profiler!=nil {
analyzer = s.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
}
slf.eventProcessor.EventHandler(ev)
s.eventProcessor.EventHandler(ev)
if analyzer!=nil {
analyzer.Pop()
analyzer = nil
}
case t := <- slf.dispatcher.ChanTimer:
case t := <- s.dispatcher.ChanTimer:
if t.IsClose() == false {
if slf.profiler != nil {
analyzer = slf.profiler.Push(fmt.Sprintf("Timer_%s", t.AdditionData.(*timer.Timer).GetFunctionName()))
if s.profiler != nil {
analyzer = s.profiler.Push(fmt.Sprintf("Timer_%s", t.AdditionData.(*timer.Timer).GetFunctionName()))
}
t.AdditionData.(*timer.Timer).Cb()
if analyzer != nil {
@@ -153,26 +150,25 @@ func (slf *Service) Run() {
}
if bStop == true {
if atomic.AddInt32(&slf.gorouterNum,-1)<=0 {
slf.startStatus = false
slf.Release()
slf.OnRelease()
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
s.startStatus = false
s.Release()
s.OnRelease()
}
break
}
}
}
func (slf *Service) GetName() string{
return slf.name
func (s *Service) GetName() string{
return s.name
}
func (slf *Service) SetName(serviceName string) {
slf.name = serviceName
func (s *Service) SetName(serviceName string) {
s.name = serviceName
}
func (slf *Service) Release(){
func (s *Service) Release(){
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 4096)
@@ -181,36 +177,37 @@ func (slf *Service) Release(){
log.Error("core dump info:%+v\n",err)
}
}()
slf.self.OnRelease()
log.Debug("Release Service %s.",slf.GetName())
s.self.OnRelease()
log.Debug("Release Service %s.", s.GetName())
}
func (slf *Service) OnRelease(){
func (s *Service) OnRelease(){
}
func (slf *Service) OnInit() error {
func (s *Service) OnInit() error {
return nil
}
func (slf *Service) Wait(){
slf.wg.Wait()
func (s *Service) Wait(){
s.wg.Wait()
}
func (slf *Service) GetServiceCfg()interface{}{
return slf.serviceCfg
func (s *Service) GetServiceCfg()interface{}{
return s.serviceCfg
}
func (slf *Service) GetProfiler() *profiler.Profiler{
return slf.profiler
func (s *Service) GetProfiler() *profiler.Profiler{
return s.profiler
}
func (slf *Service) RegEventReciverFunc(eventType event.EventType,reciver event.IEventHandler,callback event.EventCallBack){
slf.eventProcessor.RegEventReciverFunc(eventType,reciver,callback)
func (s *Service) RegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler,callback event.EventCallBack){
s.eventProcessor.RegEventReciverFunc(eventType, receiver,callback)
}
func (slf *Service) UnRegEventReciverFun(eventType event.EventType,reciver event.IEventHandler){
slf.eventProcessor.UnRegEventReciverFun(eventType,reciver)
func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler){
s.eventProcessor.UnRegEventReciverFun(eventType, receiver)
}
func (slf *Service) IsSingleCoroutine() bool {
return slf.gorouterNum == 1
func (s *Service) IsSingleCoroutine() bool {
return s.goroutineNum == 1
}