diff --git a/cluster/mesh/mesh.go b/cluster/mesh/mesh.go index 01cfb1d..a637180 100644 --- a/cluster/mesh/mesh.go +++ b/cluster/mesh/mesh.go @@ -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 } @@ -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) @@ -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") } diff --git a/cluster/node/node.go b/cluster/node/node.go index 923acae..338c3d1 100644 --- a/cluster/node/node.go +++ b/cluster/node/node.go @@ -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 @@ -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) @@ -355,7 +355,7 @@ 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") } @@ -363,8 +363,10 @@ func (n *Node) addHookListener(hook cluster.Hook, handler HookHandler) { // 执行钩子函数 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) + } } }