Skip to content

Commit

Permalink
fix(config): add lock in Set function to avoid concurrent map writes …
Browse files Browse the repository at this point in the history
…when we run several servers at once (#407)

Co-authored-by: iawia002 <[email protected]>
  • Loading branch information
caicloud-bot and iawia002 authored Dec 2, 2020
1 parent 8aa6ef5 commit f2e3b26
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/center.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit f2e3b26

Please sign in to comment.