jieguo-coder commented on code in PR #3367:
URL: https://github.com/apache/dubbo-go/pull/3367#discussion_r3397434680


##########
metadata/info/metadata_info.go:
##########
@@ -159,13 +176,29 @@ func (info *MetadataInfo) GetExportedServiceURLs() 
[]*common.URL {
 }
 
 func (info *MetadataInfo) GetSubscribedURLs() []*common.URL {
+       info.mu.RLock()
+       defer info.mu.RUnlock()
+
        res := make([]*common.URL, 0)
        for _, urls := range info.subscribedServiceURLs {
                res = append(res, urls...)
        }
        return res
 }
 
+// GetServices returns a deep copy of the Services map for safe iteration by 
external callers.
+// Each ServiceInfo is fully copied with lazy fields eagerly populated to 
prevent write-on-read races.
+func (info *MetadataInfo) GetServices() map[string]*ServiceInfo {
+       info.mu.Lock()
+       defer info.mu.Unlock()
+
+       cp := make(map[string]*ServiceInfo, len(info.Services))
+       for k, v := range info.Services {
+               cp[k] = v.DeepCopy()
+       }
+       return cp
+}
+
 func (info *MetadataInfo) ReplaceExportedServices(urls []*common.URL) {
        info.Services = make(map[string]*ServiceInfo)

Review Comment:
   好的,我现在使用了内部无锁辅助模式重构逻辑,并提取 `addServiceWithoutLock` 
来处理核心映射更新。现在,`ReplaceExportedServices` 在入口获取 `info.mu.Lock()` 一次,并在循环中安全地重用辅助函数。



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