mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
对Module树状关系,重新优化管理
This commit is contained in:
@@ -2,7 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -52,10 +51,10 @@ type BaseModule struct {
|
|||||||
CurrMaxModuleId uint32
|
CurrMaxModuleId uint32
|
||||||
corouterstatus int32 //0表示运行状态 //1释放消亡状态
|
corouterstatus int32 //0表示运行状态 //1释放消亡状态
|
||||||
|
|
||||||
rwModuleLocker *sync.Mutex
|
moduleLocker sync.RWMutex
|
||||||
ExitChan chan bool
|
ExitChan chan bool
|
||||||
WaitGroup *sync.WaitGroup
|
WaitGroup *sync.WaitGroup
|
||||||
bInit bool
|
bInit bool
|
||||||
|
|
||||||
recoverCount int8
|
recoverCount int8
|
||||||
bUnOnRun bool
|
bUnOnRun bool
|
||||||
@@ -63,7 +62,6 @@ type BaseModule struct {
|
|||||||
|
|
||||||
func (slf *BaseModule) GetRoot() IModule {
|
func (slf *BaseModule) GetRoot() IModule {
|
||||||
currentOwner := slf.GetSelf()
|
currentOwner := slf.GetSelf()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
owner := currentOwner.GetOwner()
|
owner := currentOwner.GetOwner()
|
||||||
if owner == currentOwner {
|
if owner == currentOwner {
|
||||||
@@ -73,10 +71,6 @@ func (slf *BaseModule) GetRoot() IModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) getLocker() *sync.Mutex {
|
|
||||||
return slf.rwModuleLocker
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *BaseModule) SetModuleId(moduleId uint32) {
|
func (slf *BaseModule) SetModuleId(moduleId uint32) {
|
||||||
slf.moduleId = moduleId
|
slf.moduleId = moduleId
|
||||||
}
|
}
|
||||||
@@ -86,90 +80,79 @@ func (slf *BaseModule) GetModuleId() uint32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
||||||
locker := slf.GetRoot().getBaseModule().getLocker()
|
slf.moduleLocker.RLock()
|
||||||
locker.Lock()
|
|
||||||
|
|
||||||
ret, ok := slf.mapModule[moduleId]
|
ret, ok := slf.mapModule[moduleId]
|
||||||
|
slf.moduleLocker.RUnlock()
|
||||||
if ok == false {
|
if ok == false {
|
||||||
|
|
||||||
locker.Unlock()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
locker.Unlock()
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) GetModuleCount() int {
|
func (slf *BaseModule) GetModuleCount() int {
|
||||||
locker := slf.GetRoot().getBaseModule().getLocker()
|
slf.moduleLocker.RLock()
|
||||||
locker.Lock()
|
moduleCount := len(slf.mapModule)
|
||||||
defer locker.Unlock()
|
slf.moduleLocker.RUnlock()
|
||||||
|
return moduleCount
|
||||||
return len(slf.mapModule)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) genModuleId() uint32 {
|
func (slf *BaseModule) genModuleId() uint32 {
|
||||||
slf.CurrMaxModuleId = slf.CurrMaxModuleId + 1
|
slf.CurrMaxModuleId++
|
||||||
moduleId := slf.CurrMaxModuleId
|
|
||||||
|
|
||||||
return moduleId
|
return slf.CurrMaxModuleId
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) deleteModule(moduleId uint32) bool {
|
func (slf *BaseModule) deleteModule(baseModule *BaseModule) bool {
|
||||||
module, ok := slf.mapModule[moduleId]
|
for _, subModule := range baseModule.mapModule {
|
||||||
if ok == false {
|
baseModule.deleteModule(subModule.getBaseModule())
|
||||||
GetLogger().Printf(LEVER_WARN, "RemoveModule fail %d...", moduleId)
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//协程退出
|
atomic.AddInt32(&baseModule.corouterstatus, 1)
|
||||||
atomic.AddInt32(&module.getBaseModule().corouterstatus, 1)
|
//fmt.Printf("Delete %T->%T\n", slf.GetSelf(), baseModule.GetSelf())
|
||||||
module.getBaseModule().ownerService = nil
|
baseModule.ownerService = nil
|
||||||
module.getBaseModule().mapModule = nil
|
baseModule.mapModule = nil
|
||||||
module.getBaseModule().ownerModule = nil
|
baseModule.ownerModule = nil
|
||||||
module.getBaseModule().selfModule = nil
|
baseModule.selfModule = nil
|
||||||
|
|
||||||
delete(slf.mapModule, moduleId)
|
delete(slf.mapModule, baseModule.GetModuleId())
|
||||||
|
baseModule.moduleLocker.Unlock()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) releaseModule(moduleId uint32) bool {
|
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]
|
module, ok := slf.mapModule[moduleId]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
|
slf.moduleLocker.Unlock()
|
||||||
GetLogger().Printf(LEVER_FATAL, "RemoveModule fail %d...", moduleId)
|
GetLogger().Printf(LEVER_FATAL, "RemoveModule fail %d...", moduleId)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for submoduleId, _ := range module.getBaseModule().mapModule {
|
//锁住被结点树
|
||||||
module.getBaseModule().releaseModule(submoduleId)
|
slf.LockTree(module.getBaseModule())
|
||||||
}
|
slf.deleteModule(module.getBaseModule())
|
||||||
|
|
||||||
slf.deleteModule(moduleId)
|
slf.moduleLocker.Unlock()
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *BaseModule) ReleaseModule(moduleId uint32) bool {
|
|
||||||
pRoot := slf.GetRoot()
|
|
||||||
if pRoot == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
baseModule := pRoot.getBaseModule()
|
|
||||||
if baseModule == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
//locker := slf.GetRoot().getBaseModule().getLocker()
|
|
||||||
locker := baseModule.getLocker()
|
|
||||||
if locker == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
locker.Lock()
|
|
||||||
slf.releaseModule(moduleId)
|
|
||||||
locker.Unlock()
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,11 +189,8 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
locker := slf.GetRoot().getBaseModule().getLocker()
|
|
||||||
locker.Lock()
|
|
||||||
defer locker.Unlock()
|
|
||||||
|
|
||||||
//如果没有设置,自动生成ModuleId
|
//如果没有设置,自动生成ModuleId
|
||||||
|
slf.moduleLocker.Lock()
|
||||||
var genid uint32
|
var genid uint32
|
||||||
if module.GetModuleId() == 0 {
|
if module.GetModuleId() == 0 {
|
||||||
genid = slf.genModuleId()
|
genid = slf.genModuleId()
|
||||||
@@ -236,22 +216,31 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
|
|||||||
}
|
}
|
||||||
_, ok := slf.mapModule[module.GetModuleId()]
|
_, ok := slf.mapModule[module.GetModuleId()]
|
||||||
if ok == true {
|
if ok == true {
|
||||||
|
slf.moduleLocker.Unlock()
|
||||||
GetLogger().Printf(LEVER_ERROR, "check mapModule %#v id is %d ,%d is fail...", module, module.GetModuleId(), genid)
|
GetLogger().Printf(LEVER_ERROR, "check mapModule %#v id is %d ,%d is fail...", module, module.GetModuleId(), genid)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
slf.mapModule[module.GetModuleId()] = module
|
slf.mapModule[module.GetModuleId()] = module
|
||||||
|
|
||||||
|
slf.moduleLocker.Unlock()
|
||||||
|
|
||||||
//运行模块
|
//运行模块
|
||||||
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", module)
|
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", module)
|
||||||
err := module.OnInit()
|
err := module.OnInit()
|
||||||
if err != nil {
|
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)
|
GetLogger().Printf(LEVER_ERROR, "End Init module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
|
||||||
os.Exit(-1)
|
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
|
||||||
}
|
}
|
||||||
module.getBaseModule().OnInit()
|
|
||||||
GetLogger().Printf(LEVER_INFO, "End Init module %T.", module)
|
|
||||||
|
|
||||||
|
GetLogger().Printf(LEVER_INFO, "End Init module %T.", module)
|
||||||
if module.getUnOnRun() == false {
|
if module.getUnOnRun() == false {
|
||||||
go module.RunModule(module)
|
go module.RunModule(module)
|
||||||
}
|
}
|
||||||
@@ -337,7 +326,6 @@ func (slf *BaseModule) RunModule(module IModule) {
|
|||||||
slf.WaitGroup.Add(1)
|
slf.WaitGroup.Add(1)
|
||||||
defer slf.WaitGroup.Done()
|
defer slf.WaitGroup.Done()
|
||||||
for {
|
for {
|
||||||
|
|
||||||
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
||||||
module.OnEndRun()
|
module.OnEndRun()
|
||||||
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module)
|
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module)
|
||||||
@@ -346,7 +334,6 @@ func (slf *BaseModule) RunModule(module IModule) {
|
|||||||
|
|
||||||
//每500ms检查退出
|
//每500ms检查退出
|
||||||
if timer.CheckTimeOut() {
|
if timer.CheckTimeOut() {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-slf.ExitChan:
|
case <-slf.ExitChan:
|
||||||
module.OnEndRun()
|
module.OnEndRun()
|
||||||
@@ -361,7 +348,6 @@ func (slf *BaseModule) RunModule(module IModule) {
|
|||||||
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
|
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -81,7 +80,6 @@ func (slf *BaseService) Init(iservice IService) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slf.serviceid = InstanceServiceMgr().GenServiceID()
|
slf.serviceid = InstanceServiceMgr().GenServiceID()
|
||||||
slf.BaseModule.rwModuleLocker = &sync.Mutex{}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user