Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread safety to all koanf public methods #339

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ go get -u github.com/knadh/koanf/parsers/toml
- `koanf.Parser` is a generic interface that takes raw bytes, parses, and returns a nested `map[string]interface{}`. For example, JSON and YAML parsers.
- Once loaded into koanf, configuration are values queried by a delimited key path syntax. eg: `app.server.port`. Any delimiter can be chosen.
- Configuration from multiple sources can be loaded and merged into a koanf instance, for example, load from a file first and override certain values with flags from the command line.
- The koanf instance ensures thread safety by acquiring a global `sync.RWMutex` in all public methods. This ensures that the instance can be continuously read from while allowing follow-up `Load()` operations.

With these two interface implementations, koanf can obtain configuration in any format from any source, parse it, and make it available to an application.

Expand Down
57 changes: 57 additions & 0 deletions koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"sort"
"strconv"
"sync"

"github.com/knadh/koanf/maps"
"github.com/mitchellh/copystructure"
Expand All @@ -19,6 +20,7 @@ type Koanf struct {
confMapFlat map[string]interface{}
keyMap KeyMap
conf Conf
rwMutex sync.RWMutex
}

// Conf is the Koanf configuration.
Expand Down Expand Up @@ -81,6 +83,7 @@ func NewWithConf(conf Conf) *Koanf {
confMapFlat: make(map[string]interface{}),
keyMap: make(KeyMap),
conf: conf,
rwMutex: sync.RWMutex{},
}
}

Expand Down Expand Up @@ -117,12 +120,18 @@ func (ko *Koanf) Load(p Provider, pa Parser, opts ...Option) error {
}
}

ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

return ko.merge(mp, newOptions(opts))
}

// Keys returns the slice of all flattened keys in the loaded configuration
// sorted alphabetically.
func (ko *Koanf) Keys() []string {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

out := make([]string, 0, len(ko.confMapFlat))
for k := range ko.confMapFlat {
out = append(out, k)
Expand All @@ -134,6 +143,9 @@ func (ko *Koanf) Keys() []string {
// KeyMap returns a map of flattened keys and the individual parts of the
// key as slices. eg: "parent.child.key" => ["parent", "child", "key"].
func (ko *Koanf) KeyMap() KeyMap {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

out := make(KeyMap, len(ko.keyMap))
for key, parts := range ko.keyMap {
out[key] = make([]string, len(parts))
Expand All @@ -146,19 +158,28 @@ func (ko *Koanf) KeyMap() KeyMap {
// Note that it uses maps.Copy to create a copy that uses
// json.Marshal which changes the numeric types to float64.
func (ko *Koanf) All() map[string]interface{} {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

return maps.Copy(ko.confMapFlat)
}

// Raw returns a copy of the full raw conf map.
// Note that it uses maps.Copy to create a copy that uses
// json.Marshal which changes the numeric types to float64.
func (ko *Koanf) Raw() map[string]interface{} {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

return maps.Copy(ko.confMap)
}

// Sprint returns a key -> value string representation
// of the config map with keys sorted alphabetically.
func (ko *Koanf) Sprint() string {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

b := bytes.Buffer{}
for _, k := range ko.Keys() {
b.WriteString(fmt.Sprintf("%s -> %v\n", k, ko.confMapFlat[k]))
Expand All @@ -169,6 +190,9 @@ func (ko *Koanf) Sprint() string {
// Print prints a key -> value string representation
// of the config map with keys sorted alphabetically.
func (ko *Koanf) Print() {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

fmt.Print(ko.Sprint())
}

Expand All @@ -179,6 +203,9 @@ func (ko *Koanf) Print() {
// instance with the config map `sub.a.b` where everything above
// `parent.child` are cut out.
func (ko *Koanf) Cut(path string) *Koanf {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

out := make(map[string]interface{})

// Cut only makes sense if the requested key path is a map.
Expand All @@ -199,6 +226,9 @@ func (ko *Koanf) Copy() *Koanf {
// Merge merges the config map of a given Koanf instance into
// the current instance.
func (ko *Koanf) Merge(in *Koanf) error {
ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

return ko.merge(in.Raw(), new(options))
}

Expand All @@ -207,6 +237,9 @@ func (ko *Koanf) Merge(in *Koanf) error {
// If all or part of the key path is missing, it will be created.
// If the key path is `""`, this is equivalent to Merge.
func (ko *Koanf) MergeAt(in *Koanf, path string) error {
ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

// No path. Merge the two config maps.
if path == "" {
return ko.Merge(in)
Expand All @@ -222,6 +255,9 @@ func (ko *Koanf) MergeAt(in *Koanf, path string) error {

// Set sets the value at a specific key.
func (ko *Koanf) Set(key string, val interface{}) error {
ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

// Unflatten the config map with the given key path.
n := maps.Unflatten(map[string]interface{}{
key: val,
Expand All @@ -233,6 +269,9 @@ func (ko *Koanf) Set(key string, val interface{}) error {
// Marshal takes a Parser implementation and marshals the config map into bytes,
// for example, to TOML or JSON bytes.
func (ko *Koanf) Marshal(p Parser) ([]byte, error) {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

return p.Marshal(ko.Raw())
}

Expand All @@ -248,6 +287,9 @@ func (ko *Koanf) Unmarshal(path string, o interface{}) error {
// See mitchellh/mapstructure's DecoderConfig for advanced customization
// of the unmarshal behaviour.
func (ko *Koanf) UnmarshalWithConf(path string, o interface{}, c UnmarshalConf) error {
ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

if c.DecoderConfig == nil {
c.DecoderConfig = &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
Expand Down Expand Up @@ -286,6 +328,9 @@ func (ko *Koanf) UnmarshalWithConf(path string, o interface{}, c UnmarshalConf)
// Clears all keys/values if no path is specified.
// Every empty, key on the path, is recursively deleted.
func (ko *Koanf) Delete(path string) {
ko.rwMutex.Lock()
defer ko.rwMutex.Unlock()

// No path. Erase the entire map.
if path == "" {
ko.confMap = make(map[string]interface{})
Expand All @@ -309,6 +354,9 @@ func (ko *Koanf) Delete(path string) {
// Get returns the raw, uncast interface{} value of a given key path
// in the config map. If the key path does not exist, nil is returned.
func (ko *Koanf) Get(path string) interface{} {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

// No path. Return the whole conf map.
if path == "" {
return ko.Raw()
Expand Down Expand Up @@ -342,6 +390,9 @@ func (ko *Koanf) Get(path string) interface{} {
// Slices returns a list of Koanf instances constructed out of a
// []map[string]interface{} interface at the given path.
func (ko *Koanf) Slices(path string) []*Koanf {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

out := []*Koanf{}
if path == "" {
return out
Expand Down Expand Up @@ -369,6 +420,9 @@ func (ko *Koanf) Slices(path string) []*Koanf {

// Exists returns true if the given key path exists in the conf map.
func (ko *Koanf) Exists(path string) bool {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

_, ok := ko.keyMap[path]
return ok
}
Expand All @@ -377,6 +431,9 @@ func (ko *Koanf) Exists(path string) bool {
// given path. If the path is not a map, an empty string slice is
// returned.
func (ko *Koanf) MapKeys(path string) []string {
ko.rwMutex.RLock()
defer ko.rwMutex.RUnlock()

var (
out = []string{}
o = ko.Get(path)
Expand Down
Loading