From 7702d8edaf714785cee2f7fba2b19ae5c919d923 Mon Sep 17 00:00:00 2001 From: OUYANGKAI Date: Sat, 30 Mar 2024 12:53:19 +0800 Subject: [PATCH 1/3] fix: confInfo set higher priority filepath --- source/configmap/configmap_source.go | 1 + source/configmap/configmap_source_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/source/configmap/configmap_source.go b/source/configmap/configmap_source.go index ff939501..74208cda 100755 --- a/source/configmap/configmap_source.go +++ b/source/configmap/configmap_source.go @@ -512,6 +512,7 @@ func (cmSource *configMapSource) compareUpdate(newconf map[string]interface{}, f } else if filePathPriority < priority { // lower the vale higher is the priority confInfo.Value = newConfValue + confInfo.FilePath = filePath fileConfs[key] = confInfo events = append(events, &event.Event{EventSource: ConfigMapConfigSourceConst, Key: key, EventType: event.Update, Value: newConfValue}) diff --git a/source/configmap/configmap_source_test.go b/source/configmap/configmap_source_test.go index 101b0e18..60cc857e 100755 --- a/source/configmap/configmap_source_test.go +++ b/source/configmap/configmap_source_test.go @@ -125,9 +125,9 @@ func TestDynamicConfigurations(t *testing.T) { check(err) cmSource := NewConfigMapSource() + cmSource.AddFile(filename3, 2, nil) cmSource.AddFile(filename1, 0, nil) cmSource.AddFile(filename2, 1, nil) - cmSource.AddFile(filename3, 2, nil) dynHandler := new(TestDynamicConfigHandler) cmSource.Watch(dynHandler) From 81943d36e2f52ef566d2b68097a046357e01c6eb Mon Sep 17 00:00:00 2001 From: OUYANGKAI Date: Sat, 30 Mar 2024 21:25:10 +0800 Subject: [PATCH 2/3] fix: if confInfo == nil, confInfo.Value = nil will panic --- source/configmap/configmap_source.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/configmap/configmap_source.go b/source/configmap/configmap_source.go index 74208cda..677b231b 100755 --- a/source/configmap/configmap_source.go +++ b/source/configmap/configmap_source.go @@ -283,7 +283,6 @@ func (cmSource *configMapSource) GetConfigurationByKey(key string) (interface{}, for ckey, confInfo := range cmSource.Configurations { if confInfo == nil { - confInfo.Value = nil continue } @@ -473,7 +472,6 @@ func (cmSource *configMapSource) compareUpdate(newconf map[string]interface{}, f for key, confInfo := range cmSource.Configurations { if confInfo == nil { - confInfo.Value = nil continue } From ff55903c8c4e3dc0051bb75779a031fb9a84f4e9 Mon Sep 17 00:00:00 2001 From: OUYANGKAI Date: Sat, 30 Mar 2024 21:27:00 +0800 Subject: [PATCH 3/3] remove `for range` statement to reduce time spend --- source/file/file.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/source/file/file.go b/source/file/file.go index 17dda5e3..a39d07e5 100755 --- a/source/file/file.go +++ b/source/file/file.go @@ -293,17 +293,12 @@ func (fSource *Source) GetConfigurationByKey(key string) (interface{}, error) { fSource.RLock() defer fSource.RUnlock() - for ckey, confInfo := range fSource.Configurations { - if confInfo == nil { - continue - } - - if ckey == key { - return confInfo.Value, nil - } + confInfo, ok := fSource.Configurations[key] + if !ok || confInfo == nil { + return nil, source.ErrKeyNotExist } - return nil, source.ErrKeyNotExist + return confInfo.Value, nil } //GetSourceName get name of source