From f2e3b26433ebc4c3c78e39d303328fa6bec40488 Mon Sep 17 00:00:00 2001 From: caicloud-bot Date: Wed, 2 Dec 2020 10:30:41 +0800 Subject: [PATCH] fix(config): add lock in Set function to avoid concurrent map writes when we run several servers at once (#407) Co-authored-by: iawia002 --- config/center.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/center.go b/config/center.go index e68a485c..05ae8024 100644 --- a/config/center.go +++ b/config/center.go @@ -19,13 +19,17 @@ package config import ( "os" "path/filepath" + "sync" "time" "github.com/spf13/cast" "github.com/spf13/viper" ) -var v = viper.New() +var ( + v = viper.New() + lock sync.RWMutex +) func init() { // Initialize config paths. @@ -72,6 +76,8 @@ func IsSet(key string) bool { // Will be used instead of values obtained via // flags, config file, ENV, default, or key/value store. func Set(key string, value interface{}) { + lock.Lock() + defer lock.Unlock() v.Set(key, value) }