avoid concurrent map updates to viper

This commit is contained in:
Chris Lu
2021-01-12 02:28:13 -08:00
parent 38d516251e
commit cfb9342a15
6 changed files with 26 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ package util
import (
"strings"
"sync"
"github.com/spf13/viper"
@@ -46,9 +47,20 @@ func LoadConfiguration(configFileName string, required bool) (loaded bool) {
return true
}
func GetViper() *viper.Viper {
v := &viper.Viper{}
*v = *viper.GetViper()
type ViperProxy struct {
*viper.Viper
sync.Mutex
}
func (vp *ViperProxy) SetDefault(key string, value interface{}) {
vp.Lock()
defer vp.Unlock()
vp.Viper.SetDefault(key, value)
}
func GetViper() *ViperProxy {
v := &ViperProxy{}
v.Viper = viper.GetViper()
v.AutomaticEnv()
v.SetEnvPrefix("weed")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))