Skip to content

Commit

Permalink
remove mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 14, 2023
1 parent 14617d4 commit fab4f25
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
22 changes: 3 additions & 19 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package konf
import (
"log/slog"
"reflect"
"sync"

"github.com/ktong/konf/provider/env"
)
Expand All @@ -16,9 +15,6 @@ import (
//
// The path is case-insensitive.
func Get[T any](path string) T { //nolint:ireturn
mux.RLock()
defer mux.RUnlock()

var value T
if err := global.Unmarshal(path, &value); err != nil {
slog.Error(
Expand All @@ -39,9 +35,6 @@ func Get[T any](path string) T { //nolint:ireturn
//
// The path is case-insensitive.
func Unmarshal(path string, target any) error {
mux.RLock()
defer mux.RUnlock()

return global.Unmarshal(path, target)
}

Expand All @@ -50,25 +43,16 @@ func Unmarshal(path string, target any) error {
//
// It requires Watch has been called.
func OnChange(onChange func(), paths ...string) {
mux.RLock()
defer mux.RUnlock()

global.OnChange(func(Unmarshaler) { onChange() }, paths...)
}

// SetGlobal makes c the global Config. After this call,
// SetGlobal makes config as the global Config. After this call,
// the konf package's functions (e.g. konf.Get) will read from the global config.
// This method is not concurrency-safe.
//
// The default global config only loads configuration from environment variables.
func SetGlobal(config Config) {
mux.Lock()
defer mux.Unlock()

global = config
}

//nolint:gochecknoglobals
var (
global, _ = New(WithLoader(env.New()))
mux sync.RWMutex
)
var global, _ = New(WithLoader(env.New())) //nolint:gochecknoglobals
2 changes: 2 additions & 0 deletions global_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2023 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

//go:build !race

package konf_test

import (
Expand Down

0 comments on commit fab4f25

Please sign in to comment.