Alanxtl commented on code in PR #3134:
URL: https://github.com/apache/dubbo-go/pull/3134#discussion_r2642154032
##########
config_center/file/listener.go:
##########
@@ -52,28 +53,46 @@ func NewCacheListener(rootPath string) *CacheListener {
go func() {
for {
select {
- case event := <-watch.Events:
+ case event, ok := <-watch.Events:
+ if !ok {
+ return
+ }
key := event.Name
+ if key == "" {
+ continue
+ }
logger.Debugf("watcher %s, event %v",
cl.rootPath, event)
+ if event.Op&fsnotify.Remove == fsnotify.Remove {
+ cl.contentCache.Delete(key)
+ if l, ok := cl.keyListeners.Load(key);
ok {
+
removeCallback(l.(map[config_center.ConfigurationListener]struct{}), key,
remoting.EventTypeDel)
+ }
+ }
if event.Op&fsnotify.Write == fsnotify.Write {
+ content := getFileContent(key)
+ if prev, ok :=
cl.contentCache.Load(key); ok {
+ if prevStr, ok :=
prev.(string); ok && prevStr == content {
+ continue
+ }
+ }
+ cl.contentCache.Store(key, content)
if l, ok := cl.keyListeners.Load(key);
ok {
-
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key,
+
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key,
content,
remoting.EventTypeUpdate)
}
}
if event.Op&fsnotify.Create == fsnotify.Create {
+ content := getFileContent(key)
+ cl.contentCache.Store(key, content)
if l, ok := cl.keyListeners.Load(key);
ok {
-
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key,
+
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key,
content,
remoting.EventTypeAdd)
}
}
Review Comment:
`fsnotify` 在不同平台经常会对一次写入触发多次 Write/Create。目前 `Write` 做了 `cache` 比较,但
`Create` 分支似乎没有同样的去重比较,可能导致同内容的 Add/Update 重复发出。
##########
config_center/file/listener.go:
##########
@@ -147,10 +165,17 @@ func (cl *CacheListener) RemoveListener(key string,
listener config_center.Confi
if !loaded {
return
}
- delete(listeners.(map[config_center.ConfigurationListener]struct{}),
listener)
- if err := cl.watch.Remove(key); err != nil {
- logger.Errorf("watcher remove path:%s err:%v", key, err)
+ lmap := listeners.(map[config_center.ConfigurationListener]struct{})
+ delete(lmap, listener)
+ if len(lmap) == 0 {
+ cl.keyListeners.Delete(key)
+ cl.contentCache.Delete(key)
+ if err := cl.watch.Remove(key); err != nil {
+ logger.Errorf("watcher remove path:%s err:%v", key, err)
+ }
+ return
}
+ cl.keyListeners.Store(key, lmap)
}
Review Comment:
`RemoveListener` 现在看起来会在移除任意一个 `listener` 时就执行 `cl.watch.Remove(key)`,但同一个
`key` 可能还有其他 `listener` 仍在订阅。这样会导致剩余 `listener` 永远收不到后续变更事件。
##########
config_center/zookeeper/impl.go:
##########
@@ -290,3 +291,21 @@ func (c *zookeeperDynamicConfiguration) buildPath(group
string) string {
}
return c.rootPath + pathSeparator + group
}
+
+func collapseConsecutiveSlashes(path string) string {
+ buf := make([]byte, 0, len(path))
+ prevSlash := false
+ for i := 0; i < len(path); i++ {
+ c := path[i]
+ if c == '/' {
+ if prevSlash {
+ continue
+ }
+ prevSlash = true
+ } else {
+ prevSlash = false
+ }
+ buf = append(buf, c)
+ }
+ return string(buf)
+}
Review Comment:
golang has a func called `path.Clean()`, maybe can replace this func, do not
rebuild wheel
--
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]