From 8511bf01d00ee7a353c4046f046081049b28d9bc Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Thu, 14 Oct 2021 20:29:16 -0400 Subject: [PATCH 1/2] Use a single mutex, as there is a single keys map Signed-off-by: Peter Broadhurst --- internal/config/config.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 1f977d8d18..44d2334ec5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -393,8 +393,10 @@ func MergeConfig(configRecords []*fftypes.ConfigRecord) error { return nil } +var rootKeys = map[string]bool{} +var keysMutex sync.Mutex var root = &configPrefix{ - keys: map[string]bool{}, // All keys go here, including those defined in sub prefixies + keys: rootKeys, // All keys go here, including those defined in sub prefixies } // ark adds a root key, used to define the keys that are used within the core @@ -406,8 +408,8 @@ func rootKey(k string) RootKey { // GetKnownKeys gets the known keys func GetKnownKeys() []string { var keys []string - root.keysMutex.Lock() - defer root.keysMutex.Unlock() + keysMutex.Lock() + defer keysMutex.Unlock() for k := range root.keys { keys = append(keys, k) } @@ -417,9 +419,8 @@ func GetKnownKeys() []string { // configPrefix is the main config structure passed to plugins, and used for root to wrap viper type configPrefix struct { - prefix string - keys map[string]bool - keysMutex sync.Mutex + prefix string + keys map[string]bool } // configPrefixArray is a point in the config that supports an array @@ -440,8 +441,8 @@ func NewPluginConfig(prefix string) Prefix { } func (c *configPrefix) prefixKey(k string) string { - c.keysMutex.Lock() - defer c.keysMutex.Unlock() + keysMutex.Lock() + defer keysMutex.Unlock() key := c.prefix + k if !c.keys[key] { panic(fmt.Sprintf("Undefined configuration key '%s'", key)) @@ -495,8 +496,8 @@ func (c *configPrefixArray) ArrayEntry(i int) Prefix { func (c *configPrefixArray) AddKnownKey(k string, defValue ...interface{}) { // Put a simulated key in the known keys array, to pop into the help info. - root.keysMutex.Lock() - defer root.keysMutex.Unlock() + keysMutex.Lock() + defer keysMutex.Unlock() root.keys[fmt.Sprintf("%s[].%s", c.base, k)] = true c.defaults[k] = defValue } @@ -508,8 +509,8 @@ func (c *configPrefix) AddKnownKey(k string, defValue ...interface{}) { } else if len(defValue) > 0 { c.SetDefault(k, defValue) } - c.keysMutex.Lock() - defer c.keysMutex.Unlock() + keysMutex.Lock() + defer keysMutex.Unlock() c.keys[key] = true } From e1c215d56f4460dada6d6b13395bf69c2b89456f Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Thu, 14 Oct 2021 20:34:19 -0400 Subject: [PATCH 2/2] Make it clearer there is just one list of keys Signed-off-by: Peter Broadhurst --- internal/config/config.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 44d2334ec5..df4e0aff47 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -393,11 +393,9 @@ func MergeConfig(configRecords []*fftypes.ConfigRecord) error { return nil } -var rootKeys = map[string]bool{} +var knownKeys = map[string]bool{} // All keys go here, including those defined in sub prefixies var keysMutex sync.Mutex -var root = &configPrefix{ - keys: rootKeys, // All keys go here, including those defined in sub prefixies -} +var root = &configPrefix{} // ark adds a root key, used to define the keys that are used within the core func rootKey(k string) RootKey { @@ -410,7 +408,7 @@ func GetKnownKeys() []string { var keys []string keysMutex.Lock() defer keysMutex.Unlock() - for k := range root.keys { + for k := range knownKeys { keys = append(keys, k) } sort.Strings(keys) @@ -420,7 +418,6 @@ func GetKnownKeys() []string { // configPrefix is the main config structure passed to plugins, and used for root to wrap viper type configPrefix struct { prefix string - keys map[string]bool } // configPrefixArray is a point in the config that supports an array @@ -436,7 +433,6 @@ func NewPluginConfig(prefix string) Prefix { } return &configPrefix{ prefix: prefix, - keys: root.keys, } } @@ -444,7 +440,7 @@ func (c *configPrefix) prefixKey(k string) string { keysMutex.Lock() defer keysMutex.Unlock() key := c.prefix + k - if !c.keys[key] { + if !knownKeys[key] { panic(fmt.Sprintf("Undefined configuration key '%s'", key)) } return key @@ -453,7 +449,6 @@ func (c *configPrefix) prefixKey(k string) string { func (c *configPrefix) SubPrefix(suffix string) Prefix { return &configPrefix{ prefix: c.prefix + suffix + ".", - keys: root.keys, } } @@ -477,7 +472,6 @@ func (c *configPrefixArray) ArraySize() int { func (c *configPrefixArray) ArrayEntry(i int) Prefix { cp := &configPrefix{ prefix: c.base + fmt.Sprintf(".%d.", i), - keys: root.keys, } for knownKey, defValue := range c.defaults { cp.AddKnownKey(knownKey, defValue...) @@ -498,7 +492,7 @@ func (c *configPrefixArray) AddKnownKey(k string, defValue ...interface{}) { // Put a simulated key in the known keys array, to pop into the help info. keysMutex.Lock() defer keysMutex.Unlock() - root.keys[fmt.Sprintf("%s[].%s", c.base, k)] = true + knownKeys[fmt.Sprintf("%s[].%s", c.base, k)] = true c.defaults[k] = defValue } @@ -511,7 +505,7 @@ func (c *configPrefix) AddKnownKey(k string, defValue ...interface{}) { } keysMutex.Lock() defer keysMutex.Unlock() - c.keys[key] = true + knownKeys[key] = true } func (c *configPrefix) SetDefault(k string, defValue interface{}) {