AlexStocks commented on code in PR #3367:
URL: https://github.com/apache/dubbo-go/pull/3367#discussion_r3396155611
##########
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:
[P1] 这里仍然绕过 `MetadataInfo.mu` 直接重置 `Services` 和 `exportedServiceURLs`。本 PR
已经让 `AddService`、`RemoveService`、`GetServices`、`GetExportedServiceURLs` 通过同一个
mutex 保护这些字段,但 `ReplaceExportedServices` 由 `service_discovery_registry.go`
调用时会无锁写 map;如果同时有 metadata 读取或实例变更处理在调用
`GetServices`/`GetExportedServiceURLs`,仍然可能触发 data race
或读到半重建状态。这里需要在函数入口持有写锁,并避免在持锁后直接调用会再次加锁的 `AddService`,可以拆出一个内部 no-lock helper
来复用写入逻辑。
--
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]