Aias00 commented on code in PR #3441:
URL: https://github.com/apache/dubbo-go/pull/3441#discussion_r3426426537
##########
config_center/nacos/listener.go:
##########
@@ -18,7 +18,6 @@
package nacos
import (
- "context"
"sync"
)
Review Comment:
Thanks for the review! These imports are already present in the file — the
diff only showed the first import block, but the file has three import blocks
(lines 20-22, 24-29, 31-37). The code compiles and all tests pass. No change
needed.
##########
config_center/nacos/listener.go:
##########
@@ -70,17 +68,35 @@ func (n *nacosDynamicConfiguration) addListener(key string,
listener config_cent
return
}
}
- _, cancel := context.WithCancel(context.Background())
listenersMap := rawListenersMap.(*sync.Map)
- listenersMap.Store(listener, cancel)
+ listenersMap.Store(listener, struct{}{})
}
func (n *nacosDynamicConfiguration) removeListener(key string, listener
config_center.ConfigurationListener) {
rawListenersMap, loaded := n.keyListeners.Load(key)
if !loaded {
logger.Errorf("[ConfigCenter][Nacos] key is not be listened,
key=%s", key)
- } else {
- listenersMap := rawListenersMap.(*sync.Map)
- listenersMap.Delete(listener)
+ return
+ }
+ listenersMap := rawListenersMap.(*sync.Map)
+ listenersMap.Delete(listener)
+
+ // If no listeners remain for this key, cancel the nacos config
subscription
+ isEmpty := true
+ listenersMap.Range(func(_, _ any) bool {
+ isEmpty = false
+ return false
+ })
+ if isEmpty {
+ n.keyListeners.Delete(key)
+ if n.client != nil {
+ err :=
n.client.Client().CancelListenConfig(vo.ConfigParam{
+ DataId: key,
+ Group:
n.resolvedGroup(n.url.GetParam(constant.NacosGroupKey,
constant2.DEFAULT_GROUP)),
+ })
+ if err != nil {
+ logger.Errorf("[ConfigCenter][Nacos] cancel
listen config fail, key=%s, err=%v", key, err)
+ }
+ }
}
Review Comment:
Good catch! Fixed in the latest commit. I replaced the bare `sync.Map` with
a `keyListenerSet` struct that uses `sync.Mutex` to make add/remove/check-empty
atomic. This prevents the race where a concurrent `addListener` could add a
listener between the emptiness check and the `CancelListenConfig` call.
Additionally, I now delete from `keyListeners` *before* calling
`CancelListenConfig` so that a new `addListener` won't find the stale set.
##########
config_center/nacos/listener.go:
##########
@@ -70,17 +68,35 @@ func (n *nacosDynamicConfiguration) addListener(key string,
listener config_cent
return
}
}
- _, cancel := context.WithCancel(context.Background())
listenersMap := rawListenersMap.(*sync.Map)
- listenersMap.Store(listener, cancel)
+ listenersMap.Store(listener, struct{}{})
}
func (n *nacosDynamicConfiguration) removeListener(key string, listener
config_center.ConfigurationListener) {
rawListenersMap, loaded := n.keyListeners.Load(key)
if !loaded {
logger.Errorf("[ConfigCenter][Nacos] key is not be listened,
key=%s", key)
- } else {
- listenersMap := rawListenersMap.(*sync.Map)
- listenersMap.Delete(listener)
+ return
+ }
+ listenersMap := rawListenersMap.(*sync.Map)
+ listenersMap.Delete(listener)
+
+ // If no listeners remain for this key, cancel the nacos config
subscription
+ isEmpty := true
+ listenersMap.Range(func(_, _ any) bool {
+ isEmpty = false
+ return false
+ })
+ if isEmpty {
+ n.keyListeners.Delete(key)
+ if n.client != nil {
+ err :=
n.client.Client().CancelListenConfig(vo.ConfigParam{
+ DataId: key,
+ Group:
n.resolvedGroup(n.url.GetParam(constant.NacosGroupKey,
constant2.DEFAULT_GROUP)),
+ })
Review Comment:
Valid concern. Fixed in the latest commit — the resolved `group` string is
now stored in the `keyListenerSet` struct at registration time, and
`CancelListenConfig` uses `set.group` directly instead of re-deriving it. This
guarantees the same (DataId, Group) pair is used for both `ListenConfig` and
`CancelListenConfig`.
--
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]