mirror of
https://github.com/spf13/viper
synced 2024-12-23 20:17:03 +00:00
add sync.RWMutex for goroutine safe
This commit is contained in:
parent
40e41dd224
commit
69c351ace2
1 changed files with 11 additions and 1 deletions
12
viper.go
12
viper.go
|
@ -59,7 +59,7 @@ func (e ConfigMarshalError) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var v *Viper
|
var v *Viper
|
||||||
|
var mutex sync.RWMutex
|
||||||
type RemoteResponse struct {
|
type RemoteResponse struct {
|
||||||
Value []byte
|
Value []byte
|
||||||
Error error
|
Error error
|
||||||
|
@ -499,7 +499,9 @@ func (v *Viper) searchMap(source map[string]interface{}, path []string) interfac
|
||||||
return source
|
return source
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mutex.RLock()
|
||||||
next, ok := source[path[0]]
|
next, ok := source[path[0]]
|
||||||
|
mutex.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
// Fast path
|
// Fast path
|
||||||
if len(path) == 1 {
|
if len(path) == 1 {
|
||||||
|
@ -542,7 +544,9 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path []
|
||||||
for i := len(path); i > 0; i-- {
|
for i := len(path); i > 0; i-- {
|
||||||
prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim))
|
prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim))
|
||||||
|
|
||||||
|
mutex.RLock()
|
||||||
next, ok := source[prefixKey]
|
next, ok := source[prefixKey]
|
||||||
|
mutex.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
// Fast path
|
// Fast path
|
||||||
if i == len(path) {
|
if i == len(path) {
|
||||||
|
@ -1233,7 +1237,9 @@ func (v *Viper) Set(key string, value interface{}) {
|
||||||
deepestMap := deepSearch(v.override, path[0:len(path)-1])
|
deepestMap := deepSearch(v.override, path[0:len(path)-1])
|
||||||
|
|
||||||
// set innermost value
|
// set innermost value
|
||||||
|
mutex.Lock()
|
||||||
deepestMap[lastKey] = value
|
deepestMap[lastKey] = value
|
||||||
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadInConfig will discover and load the configuration file from disk
|
// ReadInConfig will discover and load the configuration file from disk
|
||||||
|
@ -1442,7 +1448,9 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
lastKey := strings.ToLower(path[len(path)-1])
|
lastKey := strings.ToLower(path[len(path)-1])
|
||||||
deepestMap := deepSearch(c, path[0:len(path)-1])
|
deepestMap := deepSearch(c, path[0:len(path)-1])
|
||||||
// set innermost value
|
// set innermost value
|
||||||
|
mutex.Lock()
|
||||||
deepestMap[lastKey] = value
|
deepestMap[lastKey] = value
|
||||||
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1805,7 +1813,9 @@ func (v *Viper) AllSettings() map[string]interface{} {
|
||||||
lastKey := strings.ToLower(path[len(path)-1])
|
lastKey := strings.ToLower(path[len(path)-1])
|
||||||
deepestMap := deepSearch(m, path[0:len(path)-1])
|
deepestMap := deepSearch(m, path[0:len(path)-1])
|
||||||
// set innermost value
|
// set innermost value
|
||||||
|
mutex.Lock()
|
||||||
deepestMap[lastKey] = value
|
deepestMap[lastKey] = value
|
||||||
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue