From 634e8d29fc8a6bebd2cd79f062fd0eb9543bf890 Mon Sep 17 00:00:00 2001 From: cvictory <shenglicao2@gmail.com> Date: Tue, 13 Oct 2020 21:29:34 +0800 Subject: [PATCH] enhance the refresh func --- registry/directory/directory.go | 9 ++++++--- registry/protocol/protocol.go | 2 +- registry/registry.go | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/registry/directory/directory.go b/registry/directory/directory.go index c78a9f1bd..88e8189fa 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -113,8 +113,9 @@ func (dir *RegistryDirectory) Notify(event *registry.ServiceEvent) { } // NotifyAll notify the events that are complete Service Event List. -func (dir *RegistryDirectory) NotifyAll(events []*registry.ServiceEvent) { - go dir.refreshAllInvokers(events) +// After notify the address, the callback func will be invoked. +func (dir *RegistryDirectory) NotifyAll(events []*registry.ServiceEvent, callback func()) { + go dir.refreshAllInvokers(events, callback) } // refreshInvokers refreshes service's events. @@ -132,7 +133,7 @@ func (dir *RegistryDirectory) refreshInvokers(event *registry.ServiceEvent) { // refreshAllInvokers the argument is the complete list of the service events, we can safely assume any cached invoker // not in the incoming list can be removed. The Action of serviceEvent should be EventTypeUpdate. -func (dir *RegistryDirectory) refreshAllInvokers(events []*registry.ServiceEvent) { +func (dir *RegistryDirectory) refreshAllInvokers(events []*registry.ServiceEvent, callback func()) { var ( oldInvokers []protocol.Invoker addEvents []*registry.ServiceEvent @@ -145,6 +146,8 @@ func (dir *RegistryDirectory) refreshAllInvokers(events []*registry.ServiceEvent return } } + // After notify all addresses, do some callback. + defer callback() func() { // this lock is work at batch update of InvokeCache dir.registerLock.Lock() diff --git a/registry/protocol/protocol.go b/registry/protocol/protocol.go index 00f7e8b2b..a3ba3eba6 100644 --- a/registry/protocol/protocol.go +++ b/registry/protocol/protocol.go @@ -249,7 +249,7 @@ func (nl *overrideSubscribeListener) Notify(event *registry.ServiceEvent) { } } -func (nl *overrideSubscribeListener) NotifyAll(events []*registry.ServiceEvent) { +func (nl *overrideSubscribeListener) NotifyAll(events []*registry.ServiceEvent, callback func()) { if len(events) == 0 { return } diff --git a/registry/registry.go b/registry/registry.go index 97051334d..8018aadd1 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -76,7 +76,8 @@ type NotifyListener interface { // NotifyAll the events are complete Service Event List. // The argument of events []*ServiceEvent is equal to urls []*URL, The Action of serviceEvent should be EventTypeUpdate. // If your registry center can only get all urls but can't get individual event, you should use this one. - NotifyAll([]*ServiceEvent) + // After notify the address, the callback func will be invoked. + NotifyAll([]*ServiceEvent, func()) } // Listener Deprecated! -- GitLab