AsperforMias commented on code in PR #3479:
URL: https://github.com/apache/dubbo-go/pull/3479#discussion_r3536338189


##########
registry/nacos/registry.go:
##########
@@ -375,9 +386,42 @@ func (nr *nacosRegistry) LoadSubscribeInstances(url 
*common.URL, notify registry
        return nil
 }
 
+func (nr *nacosRegistry) storeInitialSubscribeInstances(serviceName string, 
groupName string, instances []model.Instance) {
+       if len(instances) == 0 {
+               return
+       }
+       copied := make([]model.Instance, len(instances))
+       copy(copied, instances)
+       nr.initialSubscribeInstances.Store(subscribeCacheKey(serviceName, 
groupName), copied)
+}
+
+func (nr *nacosRegistry) loadInitialSubscribeInstances(serviceName string, 
groupName string) ([]model.Instance, bool) {
+       value, ok := 
nr.initialSubscribeInstances.Load(subscribeCacheKey(serviceName, groupName))
+       if !ok {
+               return nil, false
+       }
+       instances, ok := value.([]model.Instance)
+       return instances, ok
+}
+
+func (nr *nacosRegistry) deleteInitialSubscribeInstances(serviceName string, 
groupName string) {
+       nr.initialSubscribeInstances.Delete(subscribeCacheKey(serviceName, 
groupName))
+}
+
+func (nr *nacosRegistry) clearInitialSubscribeInstances() {
+       nr.initialSubscribeInstances.Range(func(key any, _ any) bool {
+               nr.initialSubscribeInstances.Delete(key)
+               return true
+       })
+}
+
+func subscribeCacheKey(serviceName string, groupName string) string {
+       return serviceName + groupName
+}

Review Comment:
   Done. `subscribeCacheKey` now includes the service name length prefix, so 
service/group pairs such as `ab+c` and `a+bc` no longer collide.



##########
registry/nacos/registry.go:
##########
@@ -366,6 +376,7 @@ func (nr *nacosRegistry) LoadSubscribeInstances(url 
*common.URL, notify registry
                return perrors.New(fmt.Sprintf("could not query the instances 
for serviceName=%s,groupName=%s,error=%v",
                        serviceName, groupName, err))
        }
+       nr.storeInitialSubscribeInstances(serviceName, groupName, instances)

Review Comment:
   已补 `TestNacosRegistryInitialSubscribeSnapshotDiff`:mock `SelectAllInstances` 
先返回 A/B,断言初始 ADD;随后订阅并触发首次 callback 只返回 A,断言不会重复 ADD A,并且会对 B 发 DEL。



##########
registry/nacos/registry.go:
##########
@@ -279,13 +281,18 @@ func (nr *nacosRegistry) subscribe(serviceName string, 
notifyListener registry.N
                return perrors.New("nacosRegistry is not available.")
        }
        listener := NewNacosListenerWithServiceName(serviceName, nr.URL, 
nr.namingClient)
+       groupName := nr.GetParam(constant.RegistryGroupKey, defaultGroup)
+       if instances, ok := nr.loadInitialSubscribeInstances(serviceName, 
groupName); ok {
+               listener.setInstanceSnapshot(instances)
+       }

Review Comment:
   Done. Added `TestNacosRegistryInitialSubscribeSnapshotDiff` to cover 
`LoadSubscribeInstances -> subscribe -> Callback`: initial A/B are notified as 
ADD, then the first callback with only A emits DEL for B and does not duplicate 
ADD for A.



-- 
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