mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-15 16:34:44 +08:00
提交origin2.0版本
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/util"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ModuleMontior struct {
|
||||
mapModule *util.MapEx
|
||||
}
|
||||
|
||||
type ModuleInfo struct {
|
||||
enterStartTm int64
|
||||
mNameInfo string
|
||||
}
|
||||
|
||||
var moduleMontior ModuleMontior
|
||||
|
||||
|
||||
|
||||
func MonitorEnter(uuid string,strMonitorInfo string){
|
||||
if moduleMontior.mapModule == nil {
|
||||
return
|
||||
}
|
||||
moduleMontior.mapModule.Set(uuid, &ModuleInfo{enterStartTm:time.Now().Unix(),mNameInfo:strMonitorInfo})
|
||||
}
|
||||
|
||||
func MonitorLeave(uuid string){
|
||||
if moduleMontior.mapModule == nil {
|
||||
return
|
||||
}
|
||||
|
||||
moduleMontior.mapModule.Del(uuid)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ReportDeadFor(){
|
||||
if moduleMontior.mapModule == nil {
|
||||
return
|
||||
}
|
||||
moduleMontior.mapModule.RLockRange(func(key interface{}, value interface{}) {
|
||||
if value != nil {
|
||||
pModuleInfo := value.(*ModuleInfo)
|
||||
//超过5分钟认为dead for
|
||||
if time.Now().Unix() - pModuleInfo.enterStartTm > 300 {
|
||||
GetLogger().Printf(LEVER_FATAL, "module is %s, Dead cycle\n", pModuleInfo.mNameInfo)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func EnableDeadForMonitor(checkInterval time.Duration){
|
||||
moduleMontior.mapModule = util.NewMapEx()
|
||||
var tmInval util.Timer
|
||||
tmInval.SetupTimer(int32(checkInterval.Milliseconds()))
|
||||
go func(){
|
||||
for {
|
||||
time.Sleep(time.Second*5)
|
||||
if tmInval.CheckTimeOut(){
|
||||
ReportDeadFor()
|
||||
ReportPprof()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
func ReportPprof(){
|
||||
strReport := ""
|
||||
for _, p := range pprof.Profiles() {
|
||||
strReport += fmt.Sprintf("Name %s,count %d\n",p.Name(),p.Count())
|
||||
}
|
||||
|
||||
GetLogger().Printf(LEVER_INFO, "PProf %s\n", strReport)
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
LEVER_UNKNOW = 0
|
||||
LEVER_DEBUG = 1
|
||||
LEVER_INFO = 2
|
||||
LEVER_WARN = 3
|
||||
LEVER_ERROR = 4
|
||||
LEVER_FATAL = 5
|
||||
LEVEL_MAX = 6
|
||||
)
|
||||
|
||||
var defaultLogger = &LoggerFmt{}
|
||||
|
||||
type ILogger interface {
|
||||
Printf(level uint, format string, v ...interface{})
|
||||
Print(level uint, v ...interface{})
|
||||
SetLogLevel(level uint)
|
||||
}
|
||||
|
||||
type LoggerFmt struct {
|
||||
}
|
||||
|
||||
func (slf *LoggerFmt) Printf(level uint, format string, v ...interface{}) {
|
||||
fmt.Printf(format, v...)
|
||||
fmt.Println("")
|
||||
}
|
||||
func (slf *LoggerFmt) Print(level uint, v ...interface{}) {
|
||||
fmt.Println(v...)
|
||||
}
|
||||
func (slf *LoggerFmt) SetLogLevel(level uint) {
|
||||
//do nothing
|
||||
}
|
||||
@@ -2,361 +2,191 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/util"
|
||||
"github.com/duanhf2012/origin/util/uuid"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"github.com/duanhf2012/originnet/event"
|
||||
"github.com/duanhf2012/originnet/util/timer"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
//ModuleNone ...
|
||||
INIT_AUTO_INCREMENT = 0
|
||||
)
|
||||
const InitModuleId = 1e18
|
||||
|
||||
|
||||
type IModule interface {
|
||||
OnInit() error //Module初始化时调用
|
||||
OnRun() bool //Module运行时调用
|
||||
OnEndRun()
|
||||
SetModuleId(moduleId uint32) //手动设置ModuleId
|
||||
GetModuleId() uint32 //获取ModuleId
|
||||
GetModuleById(moduleId uint32) IModule //通过ModuleId获取Module
|
||||
AddModule(module IModule) uint32 //添加Module
|
||||
ReleaseModule(moduleId uint32) bool //释放Module
|
||||
|
||||
SetSelf(module IModule) //设置保存自己interface
|
||||
GetSelf() IModule //获取自己interface
|
||||
SetOwner(module IModule) //设置父Module
|
||||
GetOwner() IModule //获取父Module
|
||||
SetOwnerService(iservice IService) //设置拥有者服务
|
||||
GetOwnerService() IService //获取拥有者服务
|
||||
GetRoot() IModule //获取Root根Module
|
||||
|
||||
RunModule(module IModule) //手动运行Module
|
||||
InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error //手动初始化Module
|
||||
getBaseModule() *BaseModule //获取BaseModule指针
|
||||
IsInit() bool
|
||||
SetUnOnRun()
|
||||
getUnOnRun() bool
|
||||
SetModuleId(moduleId int64) bool
|
||||
GetModuleId() int64
|
||||
AddModule(module IModule) (int64,error)
|
||||
GetModule(moduleId int64) IModule
|
||||
GetAncestor()IModule
|
||||
ReleaseModule(moduleId int64)
|
||||
NewModuleId() int64
|
||||
GetParent()IModule
|
||||
OnInit() error
|
||||
OnRelease()
|
||||
getBaseModule() IModule
|
||||
GetService() IService
|
||||
GetEventChan() chan *event.Event
|
||||
}
|
||||
|
||||
type BaseModule struct {
|
||||
moduleId uint32
|
||||
|
||||
ownerService IService
|
||||
mapModule map[uint32]IModule
|
||||
ownerModule IModule
|
||||
selfModule IModule
|
||||
|
||||
CurrMaxModuleId uint32
|
||||
corouterstatus int32 //0表示运行状态 //1释放消亡状态
|
||||
//1.管理各模块树层关系
|
||||
//2.提供定时器常用工具
|
||||
type Module struct {
|
||||
moduleId int64
|
||||
parent IModule //父亲
|
||||
self IModule //父亲
|
||||
child map[int64]IModule //孩子们
|
||||
mapActiveTimer map[*timer.Timer]interface{}
|
||||
mapActiveCron map[*timer.Cron]interface{}
|
||||
|
||||
moduleLocker sync.RWMutex
|
||||
ExitChan chan bool
|
||||
WaitGroup *sync.WaitGroup
|
||||
bInit bool
|
||||
dispatcher *timer.Dispatcher //timer
|
||||
|
||||
recoverCount int8
|
||||
bUnOnRun bool
|
||||
//根结点
|
||||
ancestor IModule //始祖
|
||||
seedModuleId int64 //模块id种子
|
||||
descendants map[int64]IModule//始祖的后裔们
|
||||
|
||||
//事件管道
|
||||
event.EventProcessor
|
||||
//eventChan chan *SEvent
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetRoot() IModule {
|
||||
currentOwner := slf.GetSelf()
|
||||
for {
|
||||
owner := currentOwner.GetOwner()
|
||||
if owner == currentOwner {
|
||||
return owner
|
||||
}
|
||||
currentOwner = owner
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetModuleId(moduleId uint32) {
|
||||
slf.moduleId = moduleId
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetModuleId() uint32 {
|
||||
return slf.moduleId
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
||||
slf.moduleLocker.RLock()
|
||||
ret, ok := slf.mapModule[moduleId]
|
||||
slf.moduleLocker.RUnlock()
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetModuleCount() int {
|
||||
slf.moduleLocker.RLock()
|
||||
moduleCount := len(slf.mapModule)
|
||||
slf.moduleLocker.RUnlock()
|
||||
return moduleCount
|
||||
}
|
||||
|
||||
func (slf *BaseModule) genModuleId() uint32 {
|
||||
slf.CurrMaxModuleId++
|
||||
|
||||
return slf.CurrMaxModuleId
|
||||
}
|
||||
|
||||
func (slf *BaseModule) deleteModule(baseModule *BaseModule) bool {
|
||||
for _, subModule := range baseModule.mapModule {
|
||||
baseModule.deleteModule(subModule.getBaseModule())
|
||||
}
|
||||
|
||||
atomic.AddInt32(&baseModule.corouterstatus, 1)
|
||||
//fmt.Printf("Delete %T->%T\n", slf.GetSelf(), baseModule.GetSelf())
|
||||
baseModule.ownerService = nil
|
||||
baseModule.mapModule = nil
|
||||
baseModule.ownerModule = nil
|
||||
baseModule.selfModule = nil
|
||||
|
||||
delete(slf.mapModule, baseModule.GetModuleId())
|
||||
baseModule.moduleLocker.Unlock()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *BaseModule) LockTree(rootModule *BaseModule) {
|
||||
rootModule.moduleLocker.Lock()
|
||||
//fmt.Printf("Lock %T\n", rootModule.GetSelf())
|
||||
|
||||
for _, pModule := range rootModule.mapModule {
|
||||
slf.LockTree(pModule.getBaseModule())
|
||||
//pModule.getBaseModule().moduleLocker.Lock()
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *BaseModule) UnLockTree(rootModule *BaseModule) {
|
||||
for _, pModule := range rootModule.mapModule {
|
||||
pModule.getBaseModule().moduleLocker.Unlock()
|
||||
}
|
||||
rootModule.moduleLocker.Unlock()
|
||||
}
|
||||
|
||||
func (slf *BaseModule) ReleaseModule(moduleId uint32) bool {
|
||||
slf.moduleLocker.Lock()
|
||||
|
||||
module, ok := slf.mapModule[moduleId]
|
||||
if ok == false {
|
||||
slf.moduleLocker.Unlock()
|
||||
GetLogger().Printf(LEVER_FATAL, "RemoveModule fail %d...", moduleId)
|
||||
func (slf *Module) SetModuleId(moduleId int64) bool{
|
||||
if moduleId > 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
//锁住被结点树
|
||||
slf.LockTree(module.getBaseModule())
|
||||
slf.deleteModule(module.getBaseModule())
|
||||
|
||||
delete(slf.mapModule, moduleId)
|
||||
|
||||
//fmt.Printf("++Release %T -- %+v\n", slf.GetSelf(), slf.mapModule)
|
||||
slf.moduleLocker.Unlock()
|
||||
slf.moduleId = moduleId
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *BaseModule) IsRoot() bool {
|
||||
return slf.GetOwner() == slf.GetSelf()
|
||||
func (slf *Module) GetModuleId() int64{
|
||||
return slf.moduleId
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetSelf() IModule {
|
||||
if slf.selfModule == nil {
|
||||
return slf
|
||||
}
|
||||
|
||||
return slf.selfModule
|
||||
func (slf *Module) OnInit() error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetUnOnRun() {
|
||||
slf.bUnOnRun = true
|
||||
}
|
||||
|
||||
func (slf *BaseModule) getUnOnRun() bool {
|
||||
return slf.bUnOnRun
|
||||
}
|
||||
|
||||
func (slf *BaseModule) AddModule(module IModule) uint32 {
|
||||
//消亡状态不允许加入模块
|
||||
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
||||
GetLogger().Printf(LEVER_ERROR, "%T Cannot AddModule %T", slf.GetSelf(), module.GetSelf())
|
||||
return 0
|
||||
func (slf *Module) AddModule(module IModule) (int64,error){
|
||||
pAddModule := module.getBaseModule().(*Module)
|
||||
if pAddModule.GetModuleId()==0 {
|
||||
pAddModule.moduleId = slf.NewModuleId()
|
||||
}
|
||||
|
||||
pModule := slf.GetModuleById(module.GetModuleId())
|
||||
if pModule != nil {
|
||||
GetLogger().Printf(LEVER_ERROR, "%T Cannot AddModule %T,moduleid %d is repeat!", slf.GetSelf(), module.GetSelf(), module.GetModuleId())
|
||||
return 0
|
||||
if slf.child == nil {
|
||||
slf.child = map[int64]IModule{}
|
||||
}
|
||||
|
||||
//如果没有设置,自动生成ModuleId
|
||||
slf.moduleLocker.Lock()
|
||||
var genid uint32
|
||||
if module.GetModuleId() == 0 {
|
||||
genid = slf.genModuleId()
|
||||
module.SetModuleId(genid)
|
||||
}
|
||||
|
||||
module.getBaseModule().selfModule = module
|
||||
if slf.GetOwner() != nil {
|
||||
if slf.IsRoot() {
|
||||
//root owner为自己
|
||||
module.SetOwner(slf.GetOwner())
|
||||
} else {
|
||||
module.SetOwner(slf.GetSelf())
|
||||
}
|
||||
}
|
||||
|
||||
//设置模块退出信号捕获
|
||||
module.InitModule(slf.ExitChan, slf.WaitGroup)
|
||||
|
||||
//存入父模块中
|
||||
if slf.mapModule == nil {
|
||||
slf.mapModule = make(map[uint32]IModule)
|
||||
}
|
||||
_, ok := slf.mapModule[module.GetModuleId()]
|
||||
_,ok := slf.child[module.GetModuleId()]
|
||||
if ok == true {
|
||||
slf.moduleLocker.Unlock()
|
||||
GetLogger().Printf(LEVER_ERROR, "check mapModule %#v id is %d ,%d is fail...", module, module.GetModuleId(), genid)
|
||||
return 0
|
||||
return 0,fmt.Errorf("Exists module id %d",module.GetModuleId())
|
||||
}
|
||||
|
||||
slf.mapModule[module.GetModuleId()] = module
|
||||
pAddModule.self = module
|
||||
pAddModule.parent = slf.self
|
||||
pAddModule.dispatcher = slf.GetAncestor().getBaseModule().(*Module).dispatcher
|
||||
pAddModule.ancestor = slf.ancestor
|
||||
|
||||
slf.moduleLocker.Unlock()
|
||||
|
||||
//运行模块
|
||||
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", module)
|
||||
err := module.OnInit()
|
||||
if err != nil {
|
||||
delete(slf.mapModule, module.GetModuleId())
|
||||
GetLogger().Printf(LEVER_ERROR, "End Init module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
|
||||
return 0
|
||||
}
|
||||
initErr := module.getBaseModule().OnInit()
|
||||
if initErr != nil {
|
||||
delete(slf.mapModule, module.GetModuleId())
|
||||
GetLogger().Printf(LEVER_ERROR, "OnInit module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), initErr)
|
||||
return 0
|
||||
return 0,err
|
||||
}
|
||||
|
||||
GetLogger().Printf(LEVER_INFO, "End Init module %T.", module)
|
||||
if module.getUnOnRun() == false {
|
||||
go module.RunModule(module)
|
||||
slf.child[module.GetModuleId()] = module
|
||||
slf.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module
|
||||
|
||||
return module.GetModuleId(),nil
|
||||
}
|
||||
|
||||
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.self.OnRelease()
|
||||
for pTimer,_ := range pModule.mapActiveTimer {
|
||||
pTimer.Stop()
|
||||
}
|
||||
|
||||
return module.GetModuleId()
|
||||
}
|
||||
|
||||
func (slf *BaseModule) OnInit() error {
|
||||
slf.bInit = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *BaseModule) OnRun() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *BaseModule) OnEndRun() {
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetOwner(ownerModule IModule) {
|
||||
slf.ownerModule = ownerModule
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetSelf(module IModule) {
|
||||
slf.selfModule = module
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetOwner() IModule {
|
||||
|
||||
if slf.ownerModule == nil {
|
||||
return slf
|
||||
for pCron,_ := range pModule.mapActiveCron {
|
||||
pCron.Stop()
|
||||
}
|
||||
return slf.ownerModule
|
||||
|
||||
delete(slf.child,moduleId)
|
||||
delete (slf.ancestor.getBaseModule().(*Module).descendants,moduleId)
|
||||
|
||||
//清理被删除的Module
|
||||
pModule.self = nil
|
||||
pModule.parent = nil
|
||||
pModule.child = nil
|
||||
pModule.mapActiveTimer = nil
|
||||
pModule.mapActiveCron = nil
|
||||
pModule.dispatcher = nil
|
||||
pModule.ancestor = nil
|
||||
pModule.descendants = nil
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetOwnerService() IService {
|
||||
return slf.ownerService
|
||||
func (slf *Module) NewModuleId() int64{
|
||||
slf.ancestor.getBaseModule().(*Module).seedModuleId+=1
|
||||
return slf.ancestor.getBaseModule().(*Module).seedModuleId
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetOwnerService(iservice IService) {
|
||||
slf.ownerService = iservice
|
||||
func (slf *Module) GetAncestor()IModule{
|
||||
return slf.ancestor
|
||||
}
|
||||
|
||||
func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error {
|
||||
slf.CurrMaxModuleId = INIT_AUTO_INCREMENT
|
||||
slf.WaitGroup = pwaitGroup
|
||||
slf.ExitChan = exit
|
||||
return nil
|
||||
func (slf *Module) GetModule(moduleId int64) IModule{
|
||||
iModule,ok := slf.GetAncestor().getBaseModule().(*Module).descendants[moduleId]
|
||||
if ok == false{
|
||||
return nil
|
||||
}
|
||||
return iModule
|
||||
}
|
||||
|
||||
func (slf *BaseModule) getBaseModule() *BaseModule {
|
||||
func (slf *Module) getBaseModule() IModule{
|
||||
return slf
|
||||
}
|
||||
|
||||
func (slf *BaseModule) IsInit() bool {
|
||||
return slf.bInit
|
||||
|
||||
func (slf *Module) GetParent()IModule{
|
||||
return slf.parent
|
||||
}
|
||||
|
||||
func (slf *BaseModule) RunModule(module IModule) {
|
||||
GetLogger().Printf(LEVER_INFO, "Start Run module %T ...", module)
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
var coreInfo string
|
||||
coreInfo = string(debug.Stack())
|
||||
|
||||
coreInfo += "\n" + fmt.Sprintf("Core module is %T, try count %d. core information is %v\n", module, slf.recoverCount, r)
|
||||
GetLogger().Printf(LEVER_FATAL, coreInfo)
|
||||
slf.recoverCount += 1
|
||||
|
||||
//重试3次
|
||||
if slf.recoverCount < 10 {
|
||||
go slf.RunModule(slf.GetSelf())
|
||||
} else {
|
||||
GetLogger().Printf(LEVER_FATAL, "Routine %T.OnRun has exited!", module)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
//运行所有子模块
|
||||
timer := util.Timer{}
|
||||
timer.SetupTimer(1000)
|
||||
slf.WaitGroup.Add(1)
|
||||
defer slf.WaitGroup.Done()
|
||||
|
||||
uuidkey := uuid.Rand().HexEx()
|
||||
moduleTypeName := fmt.Sprintf("%T",module)
|
||||
for {
|
||||
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
||||
module.OnEndRun()
|
||||
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module)
|
||||
break
|
||||
}
|
||||
|
||||
//每500ms检查退出
|
||||
if timer.CheckTimeOut() {
|
||||
select {
|
||||
case <-slf.ExitChan:
|
||||
module.OnEndRun()
|
||||
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
|
||||
return
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
MonitorEnter(uuidkey,moduleTypeName)
|
||||
if module.OnRun() == false {
|
||||
module.OnEndRun()
|
||||
MonitorLeave(uuidkey)
|
||||
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
|
||||
return
|
||||
}
|
||||
|
||||
MonitorLeave(uuidkey)
|
||||
func (slf *Module) AfterFunc(d time.Duration, cb func()) *timer.Timer {
|
||||
if slf.mapActiveTimer == nil {
|
||||
slf.mapActiveTimer =map[*timer.Timer]interface{}{}
|
||||
}
|
||||
|
||||
tm := slf.dispatcher.AfterFuncEx(d,func(t *timer.Timer){
|
||||
cb()
|
||||
delete(slf.mapActiveTimer,t)
|
||||
})
|
||||
|
||||
slf.mapActiveTimer[tm] = nil
|
||||
return tm
|
||||
}
|
||||
|
||||
func (slf *Module) CronFunc(cronExpr *timer.CronExpr, cb func()) *timer.Cron {
|
||||
if slf.mapActiveCron == nil {
|
||||
slf.mapActiveCron =map[*timer.Cron]interface{}{}
|
||||
}
|
||||
|
||||
cron := slf.dispatcher.CronFuncEx(cronExpr, func(cron *timer.Cron) {
|
||||
cb()
|
||||
delete(slf.mapActiveCron,cron)
|
||||
})
|
||||
|
||||
slf.mapActiveCron[cron] = nil
|
||||
return cron
|
||||
}
|
||||
|
||||
func (slf *Module) OnRelease(){
|
||||
}
|
||||
|
||||
func (slf *Module) GetService() IService {
|
||||
return slf.GetAncestor().(IService)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,85 +1,126 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/duanhf2012/originnet/rpc"
|
||||
"github.com/duanhf2012/originnet/util/timer"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type MethodInfo struct {
|
||||
Fun reflect.Value
|
||||
ParamList []reflect.Value
|
||||
types reflect.Type
|
||||
}
|
||||
|
||||
var closeSig chan bool
|
||||
var timerDispatcherLen = 10
|
||||
|
||||
type IService interface {
|
||||
Init(Iservice IService) error
|
||||
Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{})
|
||||
GetName() string
|
||||
|
||||
OnInit() error
|
||||
OnRun() bool
|
||||
OnFetchService(iservice IService) error
|
||||
OnSetupService(iservice IService) //其他服务被安装
|
||||
OnRemoveService(iservice IService) //其他服务被安装
|
||||
|
||||
GetServiceName() string
|
||||
SetServiceName(serviceName string) bool
|
||||
GetServiceId() int
|
||||
|
||||
GetStatus() int
|
||||
IsInit() bool
|
||||
OnRelease()
|
||||
Wait()
|
||||
Start()
|
||||
GetRpcHandler() rpc.IRpcHandler
|
||||
GetServiceCfg()interface{}
|
||||
}
|
||||
|
||||
type BaseService struct {
|
||||
BaseModule
|
||||
|
||||
serviceid int
|
||||
servicename string
|
||||
Status int
|
||||
|
||||
type Service struct {
|
||||
Module
|
||||
rpc.RpcHandler //rpc
|
||||
name string //service name
|
||||
closeSig chan bool
|
||||
wg sync.WaitGroup
|
||||
this IService
|
||||
serviceCfg interface{}
|
||||
gorouterNum int32
|
||||
startStatus bool
|
||||
}
|
||||
|
||||
func (slf *BaseService) GetServiceId() int {
|
||||
return slf.serviceid
|
||||
func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) {
|
||||
slf.name = reflect.Indirect(reflect.ValueOf(iservice)).Type().Name()
|
||||
slf.dispatcher =timer.NewDispatcher(timerDispatcherLen)
|
||||
slf.this = iservice
|
||||
slf.InitRpcHandler(iservice.(rpc.IRpcHandler),getClientFun,getServerFun)
|
||||
|
||||
//初始化祖先
|
||||
slf.ancestor = iservice.(IModule)
|
||||
slf.seedModuleId =InitModuleId
|
||||
slf.descendants = map[int64]IModule{}
|
||||
slf.serviceCfg = serviceCfg
|
||||
slf.gorouterNum = 1
|
||||
slf.this.OnInit()
|
||||
}
|
||||
|
||||
func (slf *BaseService) GetServiceName() string {
|
||||
return slf.servicename
|
||||
}
|
||||
func (slf *Service) SetGoRouterNum(gorouterNum int32) bool {
|
||||
//已经开始状态不允许修改协程数量
|
||||
if slf.startStatus == true {
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *BaseService) SetServiceName(serviceName string) bool {
|
||||
slf.servicename = serviceName
|
||||
slf.gorouterNum = gorouterNum
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *BaseService) GetStatus() int {
|
||||
return slf.Status
|
||||
func (slf *Service) Start() {
|
||||
slf.startStatus = true
|
||||
for i:=int32(0);i<slf.gorouterNum;i++{
|
||||
slf.wg.Add(1)
|
||||
go func(){
|
||||
slf.Run()
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *BaseService) OnFetchService(iservice IService) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *BaseService) OnSetupService(iservice IService) {
|
||||
}
|
||||
|
||||
func (slf *BaseService) OnRemoveService(iservice IService) {
|
||||
}
|
||||
|
||||
func (slf *BaseService) Init(iservice IService) error {
|
||||
slf.ownerService = iservice
|
||||
|
||||
if iservice.GetServiceName() == "" {
|
||||
slf.servicename = fmt.Sprintf("%T", iservice)
|
||||
parts := strings.Split(slf.servicename, ".")
|
||||
if len(parts) != 2 {
|
||||
GetLogger().Printf(LEVER_ERROR, "BaseService.Init: service name is error: %q", slf.servicename)
|
||||
err := fmt.Errorf("BaseService.Init: service name is error: %q", slf.servicename)
|
||||
return err
|
||||
func (slf *Service) Run() {
|
||||
defer slf.wg.Done()
|
||||
var bStop = false
|
||||
for{
|
||||
rpcRequestChan := slf.GetRpcRequestChan()
|
||||
rpcResponeCallBack := slf.GetRpcResponeChan()
|
||||
eventChan := slf.GetEventChan()
|
||||
select {
|
||||
case <- closeSig:
|
||||
bStop = true
|
||||
case rpcRequest :=<- rpcRequestChan:
|
||||
slf.GetRpcHandler().HandlerRpcRequest(rpcRequest)
|
||||
case rpcResponeCB := <- rpcResponeCallBack:
|
||||
slf.GetRpcHandler().HandlerRpcResponeCB(rpcResponeCB)
|
||||
case event := <- eventChan:
|
||||
slf.OnEventHandler(event)
|
||||
case t := <- slf.dispatcher.ChanTimer:
|
||||
t.Cb()
|
||||
}
|
||||
|
||||
slf.servicename = parts[1]
|
||||
if bStop == true {
|
||||
if atomic.AddInt32(&slf.gorouterNum,-1)<=0 {
|
||||
slf.startStatus = false
|
||||
slf.OnRelease()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
slf.serviceid = InstanceServiceMgr().GenServiceID()
|
||||
}
|
||||
|
||||
func (slf *Service) GetName() string{
|
||||
return slf.name
|
||||
}
|
||||
|
||||
|
||||
func (slf *Service) OnRelease(){
|
||||
}
|
||||
|
||||
func (slf *Service) OnInit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (slf *Service) Wait(){
|
||||
slf.wg.Wait()
|
||||
}
|
||||
|
||||
func (slf *Service) GetServiceCfg()interface{}{
|
||||
return slf.serviceCfg
|
||||
}
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IServiceManager interface {
|
||||
Setup(s IService) bool
|
||||
Init(logger ILogger) bool
|
||||
Start() bool
|
||||
CreateServiceID() int
|
||||
}
|
||||
|
||||
type CServiceManager struct {
|
||||
genserviceid int
|
||||
localserviceMap map[string]IService
|
||||
logger ILogger
|
||||
orderLocalService []string
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) Setup(s IService) bool {
|
||||
|
||||
s.(IModule).SetOwnerService(s)
|
||||
s.(IModule).SetOwner(s.(IModule))
|
||||
s.(IModule).SetSelf(s.(IModule))
|
||||
|
||||
slf.localserviceMap[s.GetServiceName()] = s
|
||||
slf.orderLocalService = append(slf.orderLocalService, s.GetServiceName())
|
||||
|
||||
|
||||
//通知其他服务已经安装
|
||||
for _, is := range slf.localserviceMap {
|
||||
//
|
||||
is.OnSetupService(s)
|
||||
s.OnSetupService(is)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) FindService(serviceName string) IService {
|
||||
service, ok := slf.localserviceMap[serviceName]
|
||||
if ok {
|
||||
return service
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type FetchService func(s IService) error
|
||||
|
||||
func (slf *CServiceManager) FetchService(s FetchService) IService {
|
||||
for _, se := range slf.localserviceMap {
|
||||
s(se)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) Init(logger ILogger, exit chan bool, pwaitGroup *sync.WaitGroup) bool {
|
||||
slf.logger = logger
|
||||
for _, s := range slf.localserviceMap {
|
||||
err := (s.(IModule)).InitModule(exit, pwaitGroup)
|
||||
if err != nil {
|
||||
slf.logger.Print(LEVER_FATAL, err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) Start() bool {
|
||||
for _, sname := range slf.orderLocalService {
|
||||
s := slf.FindService(sname)
|
||||
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", s.(IModule))
|
||||
err := s.(IModule).OnInit()
|
||||
if err != nil {
|
||||
GetLogger().Printf(LEVER_ERROR, "Init module %T id is %d is fail,reason:%v...", s.(IModule), s.(IModule).GetModuleId(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
s.(IModule).getBaseModule().OnInit()
|
||||
GetLogger().Printf(LEVER_INFO, "End Init module %T.", s.(IModule))
|
||||
}
|
||||
|
||||
for _, sname := range slf.orderLocalService {
|
||||
s := slf.FindService(sname)
|
||||
go (s.(IModule)).RunModule(s.(IModule))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) GenServiceID() int {
|
||||
slf.genserviceid += 1
|
||||
return slf.genserviceid
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) GetLogger() ILogger {
|
||||
ret := slf.logger
|
||||
if ret == nil {
|
||||
ret = defaultLogger
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var self *CServiceManager
|
||||
|
||||
func InstanceServiceMgr() *CServiceManager {
|
||||
if self == nil {
|
||||
self = new(CServiceManager)
|
||||
self.localserviceMap = make(map[string]IService)
|
||||
return self
|
||||
}
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
func GetLogger() ILogger {
|
||||
return InstanceServiceMgr().GetLogger()
|
||||
}
|
||||
|
||||
func (slf *CServiceManager) IsFinishInit() bool {
|
||||
for _, val := range slf.localserviceMap {
|
||||
if val.IsInit() == false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
53
service/servicemgr.go
Normal file
53
service/servicemgr.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package service
|
||||
|
||||
//本地所有的service
|
||||
var mapServiceName map[string]IService
|
||||
|
||||
func init(){
|
||||
mapServiceName = map[string]IService{}
|
||||
}
|
||||
|
||||
func Init(chanCloseSig chan bool) {
|
||||
closeSig=chanCloseSig
|
||||
|
||||
for _,s := range mapServiceName {
|
||||
s.OnInit()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func Setup(s IService) bool {
|
||||
|
||||
_,ok := mapServiceName[s.GetName()]
|
||||
if ok == true {
|
||||
return false
|
||||
}
|
||||
|
||||
//s.Init(s)
|
||||
mapServiceName[s.GetName()] = s
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func GetService(servicename string) IService {
|
||||
s,ok := mapServiceName[servicename]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
func Start(){
|
||||
for _,s := range mapServiceName {
|
||||
s.Start()
|
||||
}
|
||||
}
|
||||
|
||||
func WaitStop(){
|
||||
for _,s := range mapServiceName {
|
||||
s.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user