Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Nov 4, 2024
1 parent 5d0bbae commit b892e39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions cluster/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Mesh struct {
services []*serviceEntity
instance *registry.ServiceInstance
instances []*registry.ServiceInstance
hooks map[cluster.Hook]HookHandler
hooks map[cluster.Hook][]HookHandler
transporter transport.Server
}

Expand All @@ -42,7 +42,7 @@ func NewMesh(opts ...Option) *Mesh {

m := &Mesh{}
m.opts = o
m.hooks = make(map[cluster.Hook]HookHandler)
m.hooks = make(map[cluster.Hook][]HookHandler)
m.services = make([]*serviceEntity, 0)
m.instances = make([]*registry.ServiceInstance, 0)
m.proxy = newProxy(m)
Expand Down Expand Up @@ -183,15 +183,17 @@ func (m *Mesh) getState() cluster.State {

// 执行钩子函数
func (m *Mesh) runHookFunc(hook cluster.Hook) {
if handler, ok := m.hooks[hook]; ok {
handler(m.proxy)
if handlers, ok := m.hooks[hook]; ok {
for _, handler := range handlers {
handler(m.proxy)
}
}
}

// 添加钩子监听器
func (m *Mesh) addHookListener(hook cluster.Hook, handler HookHandler) {
if m.getState() == cluster.Shut {
m.hooks[hook] = handler
m.hooks[hook] = append(m.hooks[hook], handler)
} else {
log.Warnf("mesh server is working, can't add hook handler")
}
Expand Down
12 changes: 7 additions & 5 deletions cluster/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Node struct {
router *Router
trigger *Trigger
proxy *Proxy
hooks map[cluster.Hook]HookHandler
hooks map[cluster.Hook][]HookHandler
services []*serviceEntity
instances []*registry.ServiceInstance
linker *node.Server
Expand All @@ -57,7 +57,7 @@ func NewNode(opts ...Option) *Node {
n.router = newRouter(n)
n.trigger = newTrigger(n)
n.scheduler = newScheduler(n)
n.hooks = make(map[cluster.Hook]HookHandler)
n.hooks = make(map[cluster.Hook][]HookHandler)
n.services = make([]*serviceEntity, 0)
n.instances = make([]*registry.ServiceInstance, 0)
n.fnChan = make(chan func(), 4096)
Expand Down Expand Up @@ -355,16 +355,18 @@ func (n *Node) updateState(state cluster.State) (err error) {
// 添加钩子监听器
func (n *Node) addHookListener(hook cluster.Hook, handler HookHandler) {
if n.getState() == cluster.Shut {
n.hooks[hook] = handler
n.hooks[hook] = append(n.hooks[hook], handler)
} else {
log.Warnf("node server is working, can't add hook handler")
}
}

// 执行钩子函数
func (n *Node) runHookFunc(hook cluster.Hook) {
if handler, ok := n.hooks[hook]; ok {
handler(n.proxy)
if handlers, ok := n.hooks[hook]; ok {
for _, handler := range handlers {
handler(n.proxy)
}
}
}

Expand Down

0 comments on commit b892e39

Please sign in to comment.