Skip to content

Commit

Permalink
1.优化服务配置检查
Browse files Browse the repository at this point in the history
2.废弃SetGoRoutineNum接口
3.释放Module优化
  • Loading branch information
duanhf2012 committed Dec 6, 2024
1 parent cf6ca04 commit ffcc5a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
11 changes: 7 additions & 4 deletions cluster/parsecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,12 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
return nil
}

func (cls *Cluster) parseLocalCfg() {
func (cls *Cluster) parseLocalCfg() error{
rpcInfo := NodeRpcInfo{}
rpcInfo.nodeInfo = cls.localNodeInfo
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)

cls.mapRpc[cls.localNodeInfo.NodeId] = &rpcInfo

for _, serviceName := range cls.localNodeInfo.ServiceList {
splitServiceName := strings.Split(serviceName, ":")
if len(splitServiceName) == 2 {
Expand All @@ -440,8 +439,13 @@ func (cls *Cluster) parseLocalCfg() {
cls.mapServiceNode[serviceName] = make(map[string]struct{})
}

if _,ok:=cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId];ok {
return fmt.Errorf("duplicate service %s is configured in node %s", serviceName, cls.localNodeInfo.NodeId)
}
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
}

return nil
}

func (cls *Cluster) IsNatsMode() bool {
Expand Down Expand Up @@ -474,8 +478,7 @@ func (cls *Cluster) InitCfg(localNodeId string) error {
}

//本地配置服务加到全局map信息中
cls.parseLocalCfg()
return nil
return cls.parseLocalCfg()
}

func (cls *Cluster) IsConfigService(serviceName string) bool {
Expand Down
28 changes: 16 additions & 12 deletions service/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/duanhf2012/origin/v2/log"
rpcHandle "github.com/duanhf2012/origin/v2/rpc"
"github.com/duanhf2012/origin/v2/util/timer"
"slices"
)

const InitModuleId = 1e9
Expand Down Expand Up @@ -46,7 +47,7 @@ type Module struct {
moduleName string //模块名称
parent IModule //父亲
self IModule //自己
child map[uint32]IModule //孩子们
child []IModule //孩子们
mapActiveTimer map[timer.ITimer]struct{}
mapActiveIdTimer map[uint64]timer.ITimer
dispatcher *timer.Dispatcher //timer
Expand Down Expand Up @@ -93,10 +94,7 @@ func (m *Module) AddModule(module IModule) (uint32, error) {
pAddModule.moduleId = m.NewModuleId()
}

if m.child == nil {
m.child = map[uint32]IModule{}
}
_, ok := m.child[module.GetModuleId()]
_,ok := m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()]
if ok == true {
return 0, fmt.Errorf("exists module id %d", module.GetModuleId())
}
Expand All @@ -109,24 +107,27 @@ func (m *Module) AddModule(module IModule) (uint32, error) {
pAddModule.eventHandler = event.NewEventHandler()
pAddModule.eventHandler.Init(m.eventHandler.GetEventProcessor())
pAddModule.IConcurrent = m.IConcurrent

m.child = append(m.child,module)
m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module

err := module.OnInit()
if err != nil {
delete(m.ancestor.getBaseModule().(*Module).descendants, module.GetModuleId())
m.child = m.child[:len(m.child)-1]
log.Error("module OnInit error",log.String("ModuleName",module.GetModuleName()),log.ErrorField("err",err))
return 0, err
}

m.child[module.GetModuleId()] = module
m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module

log.Debug("Add module " + module.GetModuleName() + " completed")
return module.GetModuleId(), nil
}

func (m *Module) ReleaseModule(moduleId uint32) {
pModule := m.GetModule(moduleId).getBaseModule().(*Module)

//释放子孙
for id := range pModule.child {
m.ReleaseModule(id)
for i:=len(pModule.child)-1; i>=0; i-- {
m.ReleaseModule(pModule.child[i].GetModuleId())
}

pModule.self.OnRelease()
Expand All @@ -140,7 +141,10 @@ func (m *Module) ReleaseModule(moduleId uint32) {
t.Cancel()
}

delete(m.child, moduleId)
m.child = slices.DeleteFunc(m.child, func(module IModule) bool {
return module.GetModuleId() == moduleId
})

delete(m.ancestor.getBaseModule().(*Module).descendants, moduleId)

//清理被删除的Module
Expand Down
6 changes: 5 additions & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,13 @@ func (s *Service) Release() {
}
}()

for i:=len(s.child)-1; i>=0; i-- {
s.ReleaseModule(s.child[i].GetModuleId())
}

if atomic.AddInt32(&s.isRelease, -1) == -1 {
s.self.OnRelease()
}

}

func (s *Service) OnRelease() {
Expand Down Expand Up @@ -432,6 +435,7 @@ func (s *Service) SetEventChannelNum(num int) {
}
}

// Deprecated: replace it with the OpenConcurrent function
func (s *Service) SetGoRoutineNum(goroutineNum int32) bool {
//已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程
if s.startStatus == true || s.profiler != nil {
Expand Down

0 comments on commit ffcc5a3

Please sign in to comment.