Skip to content

Commit

Permalink
+ added finalizer that closes the re-load goroutine, for safety
Browse files Browse the repository at this point in the history
  • Loading branch information
bogcon committed Jul 4, 2022
1 parent 53410dc commit 4389315
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
36 changes: 26 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package xconf

import (
"reflect"
"runtime"
"strings"
"sync"
"time"
Expand All @@ -30,6 +31,10 @@ type Config interface {
// Is implements io.Closer interface and thus Close should be called at
// your application shutdown in order to avoid memory leaks.
type DefaultConfig struct {
*defaultConfig // so we can use finalizer
}

type defaultConfig struct {
// loader to retrieve configuration from.
loader Loader
// configMap the loaded key-value configuration map.
Expand Down Expand Up @@ -58,10 +63,10 @@ type DefaultConfig struct {
// The first parameter is the loader used as a source of getting the key-value configuration map.
// The second parameter represents a list of optional functions to configure the object.
func NewDefaultConfig(loader Loader, opts ...DefaultConfigOption) (*DefaultConfig, error) {
config := &DefaultConfig{
config := &DefaultConfig{&defaultConfig{
loader: loader,
mu: new(sync.RWMutex),
}
}}

// apply options, if any.
for _, opt := range opts {
Expand All @@ -79,6 +84,9 @@ func NewDefaultConfig(loader Loader, opts ...DefaultConfigOption) (*DefaultConfi
config.closed = make(chan struct{}, 1)
config.wg.Add(1)
go config.reloadAsync()
// register also a finalizer, just in case, user forgets to call Close().
// Note: user should do not rely on this, it's recommended to explicitly call Close().
runtime.SetFinalizer(config, (*DefaultConfig).Close)
}

return config, nil
Expand All @@ -93,7 +101,7 @@ func NewDefaultConfig(loader Loader, opts ...DefaultConfigOption) (*DefaultConfi
// Only basic types (string, bool, int, uint, float, and their flavours),
// time.Duration, time.Time, []int, []string are covered.
// If a cast error occurs, the defaultValue is returned.
func (cfg *DefaultConfig) Get(key string, def ...interface{}) interface{} {
func (cfg *defaultConfig) Get(key string, def ...interface{}) interface{} {
if cfg.ignoreCaseSensitivity {
key = strings.ToUpper(key)
}
Expand Down Expand Up @@ -122,7 +130,7 @@ func (cfg *DefaultConfig) Get(key string, def ...interface{}) interface{} {
}

// RegisterObserver adds a new observer that will get notified of keys changes.
func (cfg *DefaultConfig) RegisterObserver(observer ConfigObserver) {
func (cfg *defaultConfig) RegisterObserver(observer ConfigObserver) {
cfg.mu.Lock()
if cfg.observers == nil {
cfg.observers = []ConfigObserver{observer}
Expand All @@ -133,7 +141,7 @@ func (cfg *DefaultConfig) RegisterObserver(observer ConfigObserver) {
}

// setConfigMap loads the config map.
func (cfg *DefaultConfig) setConfigMap() error {
func (cfg *defaultConfig) setConfigMap() error {
newConfigMap, err := cfg.loader.Load()
if err != nil {
return err
Expand All @@ -154,7 +162,7 @@ func (cfg *DefaultConfig) setConfigMap() error {

// notifyObservers computes changed (updated/deleted/new) keys on a config reload,
// and notifies registered observers about them, if there are any changed keys and observers.
func (cfg *DefaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]interface{}) {
func (cfg *defaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]interface{}) {
cfg.mu.RLock()
defer cfg.mu.RUnlock()

Expand Down Expand Up @@ -184,7 +192,7 @@ func (cfg *DefaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]

// reloadAsync reloads the config map asynchronous, interval based.
// Calling Close() will stop this goroutine.
func (cfg *DefaultConfig) reloadAsync() {
func (cfg *defaultConfig) reloadAsync() {
defer cfg.wg.Done()

for {
Expand All @@ -201,13 +209,21 @@ func (cfg *DefaultConfig) reloadAsync() {
}
}

// close stops the underlying ticker used to reload config, avoiding memory leaks.
func (cfg *defaultConfig) close() {
if cfg != nil {
close(cfg.closed)
cfg.wg.Wait()
}
}

// Close stops the underlying ticker used to reload config, avoiding memory leaks.
// It should be called at your application shutdown.
// It implements io.Closer interface, and the returned error can be disregarded (is nil all the time).
func (cfg *DefaultConfig) Close() error {
if cfg.reloadInterval > 0 {
close(cfg.closed)
cfg.wg.Wait()
if cfg != nil && cfg.reloadInterval > 0 {
cfg.close()
runtime.SetFinalizer(cfg, nil)
}

return nil
Expand Down
39 changes: 35 additions & 4 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
Expand All @@ -25,6 +26,7 @@ func TestNewDefaultConfig(t *testing.T) {

t.Run("valid object", testNewDefaultConfigReturnsValidObject)
t.Run("error", testNewDefaultConfigReturnsError)
t.Run("finalizer is called", testNewDefaultConfigFinalizerIsCalled)
}

func testNewDefaultConfigReturnsValidObject(t *testing.T) {
Expand Down Expand Up @@ -68,6 +70,35 @@ func testNewDefaultConfigReturnsError(t *testing.T) {
assertNil(t, result)
}

func testNewDefaultConfigFinalizerIsCalled(t *testing.T) {
t.Parallel()

// test finalizer is called if we "forget" to call Close.
// arrange
var (
callsCnt uint32
loader = xconf.LoaderFunc(func() (map[string]interface{}, error) {
atomic.AddUint32(&callsCnt, 1)
if atomic.LoadUint32(&callsCnt) == 1 {
return map[string]interface{}{"foo": "bar"}, nil
}

return map[string]interface{}{"foo": "baz"}, nil
})
_, err = xconf.NewDefaultConfig(
loader,
xconf.DefaultConfigWithReloadInterval(700*time.Millisecond),
)
)
requireNil(t, err)

// act
runtime.GC()
time.Sleep(900 * time.Millisecond)

assertEqual(t, uint32(1), atomic.LoadUint32(&callsCnt))
}

func TestDefaultConfig_Get(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1566,8 +1597,8 @@ func TestDefaultConfig_RegisterObserver(t *testing.T) {

// setup 2 observers
observer1CallsCnt, observer2CallsCnt := 0, 0
subject.RegisterObserver(configObserverFactory(t, subject, &observer1CallsCnt))
subject.RegisterObserver(configObserverFactory(t, subject, &observer2CallsCnt))
subject.RegisterObserver(configObserverFactory(t, &observer1CallsCnt))
subject.RegisterObserver(configObserverFactory(t, &observer2CallsCnt))

// first act & assert
result1 := subject.Get("XCONF_TEST_DEFAULT_CONFIG_FOO_UPDATED")
Expand Down Expand Up @@ -1606,12 +1637,12 @@ func TestDefaultConfig_RegisterObserver(t *testing.T) {
assertEqual(t, 1, observer2CallsCnt)
}

func configObserverFactory(t *testing.T, expectedCfg xconf.Config, observerCallsCount *int) xconf.ConfigObserver {
func configObserverFactory(t *testing.T, observerCallsCount *int) xconf.ConfigObserver {
return func(cfg xconf.Config, changedKeys ...string) {
*observerCallsCount++

// check params
assertEqual(t, expectedCfg, cfg)
assertNotNil(t, cfg)
expectedChangedKeys := map[string]struct{}{
"XCONF_TEST_DEFAULT_CONFIG_FOO_UPDATED": {},
"XCONF_TEST_DEFAULT_CONFIG_FOO_DELETED": {},
Expand Down

0 comments on commit 4389315

Please sign in to comment.