From fab4f2559bcb63fef0d86e7c3a01eaff0bbd2fc4 Mon Sep 17 00:00:00 2001 From: ktong Date: Mon, 13 Nov 2023 21:23:24 -0800 Subject: [PATCH] remove mutex --- global.go | 22 +++------------------- global_test.go | 2 ++ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/global.go b/global.go index ed34ad5c..97fae2d7 100644 --- a/global.go +++ b/global.go @@ -6,7 +6,6 @@ package konf import ( "log/slog" "reflect" - "sync" "github.com/ktong/konf/provider/env" ) @@ -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( @@ -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) } @@ -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 diff --git a/global_test.go b/global_test.go index c2eb559d..943fb35b 100644 --- a/global_test.go +++ b/global_test.go @@ -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 (