Skip to content

Commit

Permalink
adding mutex to protect configCache map, fixes race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Apr 18, 2017
1 parent 39a57b4 commit dd2cdd3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion system/db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"strings"
"sync"

"github.com/ponzu-cms/ponzu/system/admin/config"

Expand All @@ -18,6 +19,7 @@ const (
DefaultMaxAge = int64(60 * 60 * 24 * 30)
)

var mu = &sync.Mutex{}
var configCache map[string]interface{}

func init() {
Expand Down Expand Up @@ -115,7 +117,9 @@ func SetConfig(data url.Values) error {
return err
}

mu.Lock()
configCache = kv
mu.Unlock()

return nil
}
Expand Down Expand Up @@ -215,7 +219,11 @@ func PutConfig(key string, value interface{}) error {
// ConfigCache is a in-memory cache of the Configs for quicker lookups
// 'key' is the JSON tag associated with the config field
func ConfigCache(key string) interface{} {
return configCache[key]
mu.Lock()
val := configCache[key]
mu.Unlock()

return val
}

// LoadCacheConfig loads the config into a cache to be accessed by ConfigCache()
Expand All @@ -239,7 +247,9 @@ func LoadCacheConfig() error {
return err
}

mu.Lock()
configCache = kv
mu.Unlock()

return nil
}
Expand Down

0 comments on commit dd2cdd3

Please sign in to comment.