From 331cea73db92eba3ceaa85e1e77d1492df29cfde Mon Sep 17 00:00:00 2001 From: tiecheng <18768171164@163.com> Date: Mon, 7 Sep 2020 14:33:34 +0800 Subject: [PATCH] remove callback add watcher error print warn --- config_center/file/impl_test.go | 18 ++++-------------- config_center/file/listener.go | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/config_center/file/impl_test.go b/config_center/file/impl_test.go index 5dbb9cadc..a87cca282 100644 --- a/config_center/file/impl_test.go +++ b/config_center/file/impl_test.go @@ -76,19 +76,11 @@ func TestAddListener(t *testing.T) { value = "Test Value 2" err = file.PublishConfig(key, group, value) assert.NoError(t, err) - - listener.wg.Add(1) - value = "Test Value 3" - err = file.PublishConfig(key, group, value) - assert.NoError(t, err) - listener.wg.Wait() - - time.Sleep(time.Second) defer destroy(file.rootPath, file) } -func TestAddAndRemoveListener(t *testing.T) { +func TestRemoveListener(t *testing.T) { file, err := initFileData(t) group := "dubbogo" value := "Test Value" @@ -103,19 +95,17 @@ func TestAddAndRemoveListener(t *testing.T) { err = file.PublishConfig(key, group, value) assert.NoError(t, err) - // sleep, make sure callback run success, do `l.wg.Done()` + // make sure callback before RemoveListener time.Sleep(time.Second) - file.RemoveListener(key, listener, config_center.WithGroup(group)) listener.wg.Add(1) + file.RemoveListener(key, listener, config_center.WithGroup(group)) value = "Test Value 3" err = file.PublishConfig(key, group, value) assert.NoError(t, err) listener.wg.Done() listener.wg.Wait() - - time.Sleep(time.Second) defer destroy(file.rootPath, file) } @@ -158,8 +148,8 @@ func TestPublishConfig(t *testing.T) { } func destroy(path string, fdc *FileSystemDynamicConfiguration) { - os.RemoveAll(path) fdc.Close() + os.RemoveAll(path) } type mockDataListener struct { diff --git a/config_center/file/listener.go b/config_center/file/listener.go index 6a0c01260..7e5a5ddd9 100644 --- a/config_center/file/listener.go +++ b/config_center/file/listener.go @@ -55,21 +55,21 @@ func NewCacheListener(rootPath string) *CacheListener { logger.Debugf("watcher %s, event %v", cl.rootPath, event) if event.Op&fsnotify.Write == fsnotify.Write { if l, ok := cl.keyListeners.Load(key); ok { - allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeUpdate) + dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeUpdate) } } if event.Op&fsnotify.Create == fsnotify.Create { if l, ok := cl.keyListeners.Load(key); ok { - allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeAdd) + dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeAdd) } } if event.Op&fsnotify.Remove == fsnotify.Remove { if l, ok := cl.keyListeners.Load(key); ok { - allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeDel) + removeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeDel) } } case err := <-watch.Errors: - logger.Errorf("file : listen watch fail:", err) + logger.Warnf("file : listen watch fail:", err) } } }() @@ -82,7 +82,16 @@ func NewCacheListener(rootPath string) *CacheListener { return cl } -func allCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) { +func removeCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) { + if len(lmap) == 0 { + logger.Warnf("file watch callback but configuration listener is empty, key:%s, event:%v", key, event) + } + for l := range lmap { + callback(l, key, "", event) + } +} + +func dataChangeCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) { if len(lmap) == 0 { logger.Warnf("file watch callback but configuration listener is empty, key:%s, event:%v", key, event) } @@ -105,6 +114,7 @@ func (cl *CacheListener) Close() { } // AddListener will add a listener if loaded +// if you watcher a file or directory not exist, will error with no such file or directory func (cl *CacheListener) AddListener(key string, listener config_center.ConfigurationListener) { // reference from https://stackoverflow.com/questions/34018908/golang-why-dont-we-have-a-set-datastructure // make a map[your type]struct{} like set in java -- GitLab