huynhhongan2005 commented on code in PR #3827:
URL: https://github.com/apache/dubbo-go/pull/3827#discussion_r4582995413


##########
registry/servicediscovery/service_discovery_registry.go:
##########
@@ -247,30 +248,36 @@ func (s *serviceDiscoveryRegistry) UnRegisterService() 
error {
 
 func (s *serviceDiscoveryRegistry) unregisterService(targetURL *common.URL) 
error {
        s.lock.Lock()
-       keep := s.instances[:0]
-       origin := s.instances[:]
+       origin := append([]registry.ServiceInstance(nil), s.instances...)
        s.lock.Unlock()
 
        var errs []error
-       keepInstanceURLs := make(map[registry.ServiceInstance]*common.URL)
+       unregistered := make(map[registry.ServiceInstance]struct{}, len(origin))
 
        for _, v := range origin {
                if err := s.serviceDiscovery.Unregister(v); err != nil {
-                       // fail to unregister
-                       keep = append(keep, v)
                        errs = append(errs, err)
-                       s.lock.RLock()
-                       if sourceURL, ok := s.instanceURLs[v]; ok {
-                               keepInstanceURLs[v] = sourceURL
-                       }
-                       s.lock.RUnlock()
+               } else {
+                       unregistered[v] = struct{}{}
                }
        }
 
        s.lock.Lock()
-       s.instances = keep
-       s.instanceURLs = keepInstanceURLs
+       newInstances := make([]registry.ServiceInstance, 0, len(s.instances))
+       var keep []registry.ServiceInstance
+       for _, inst := range s.instances {
+               if _, ok := unregistered[inst]; ok {
+                       delete(s.instanceURLs, inst)
+               } else {
+                       newInstances = append(newInstances, inst)
+                       if slices.Contains(origin, inst) {
+                               keep = append(keep, inst)
+                       }
+               }
+       }
+       s.instances = newInstances
        s.lock.Unlock()
+
        if err := s.syncExportedMetadataAfterUnregister(targetURL, origin, 
keep); err != nil {

Review Comment:
   Agreed - guarding `Register`, `RegisterService`, and `unregisterService` 
under `serviceLifecycleMu` also serializes all writes to 
`MetadataInfo.Revision` across registration and unregistration paths. I've 
added `TestServiceDiscoveryRegistry_ConcurrentRegisterAndUnregisterSameAddress` 
and verified both concurrent tests pass 100/100 runs under `go test -race`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to