Similarityoung commented on code in PR #919:
URL: https://github.com/apache/dubbo-go-pixiu/pull/919#discussion_r3176532906
##########
pkg/server/cluster_manager.go:
##########
@@ -143,16 +143,27 @@ func (cm *ClusterManager) NewStore(version int32)
*ClusterStore {
return &ClusterStore{Version: version, clustersMap:
map[string]*cluster.Cluster{}}
}
+// CompareAndSetStore swaps the store only when versions match.
+// Version mismatch must leave both stores and runtime clusters untouched.
func (cm *ClusterManager) CompareAndSetStore(store *ClusterStore) bool {
cm.rw.Lock()
- defer cm.rw.Unlock()
if store.Version != cm.store.Version {
+ cm.rw.Unlock()
return false
}
- store.carryOverRuntimeStateFrom(cm.store)
+ currentStore := cm.store
+ replacedClusters := store.ensureRuntimeClusters()
+ store.carryOverRuntimeStateFrom(currentStore)
cm.store = store
+ if store != currentStore {
+ replacedClusters = append(replacedClusters,
currentStore.runtimeClustersNotIn(store)...)
+ }
+ cm.rw.Unlock()
Review Comment:
建议:这里是否可以把加锁内的逻辑抽成一个小 helper,并在 helper 里用 `defer cm.rw.Unlock()`?
现在手动 `Unlock()` 的逻辑是对的,而且 `stopClusters()` 也保持在锁外执行。但如果后续这里新增其他 return
分支,可能会有漏掉 `Unlock()` 的风险。抽成 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]