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

adding Unwatch function for file provider #245

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions providers/file/file.go
Expand Up @@ -16,6 +16,7 @@ import (
// File implements a File provider.
type File struct {
path string
w *fsnotify.Watcher
}

// Provider returns a file provider.
Expand All @@ -36,6 +37,11 @@ func (f *File) Read() (map[string]interface{}, error) {
// Watch watches the file and triggers a callback when it changes. It is a
// blocking function that internally spawns a goroutine to watch for changes.
func (f *File) Watch(cb func(event interface{}, err error)) error {
// If a watcher already exists, return an error.
if f.w != nil {
return errors.New("watcher already exists")
}

// Resolve symlinks and save the original path so that changes to symlinks
// can be detected.
realPath, err := filepath.EvalSymlinks(f.path)
Expand All @@ -48,7 +54,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
// the whole parent directory to pick up all events such as symlink changes.
fDir, _ := filepath.Split(f.path)

w, err := fsnotify.NewWatcher()
f.w, err = fsnotify.NewWatcher()
rhnvrm marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand All @@ -62,7 +68,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
loop:
for {
select {
case event, ok := <-w.Events:
case event, ok := <-f.w.Events:
if !ok {
cb(nil, errors.New("fsnotify watch channel closed"))
break loop
Expand Down Expand Up @@ -108,7 +114,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
cb(nil, nil)

// There's an error.
case err, ok := <-w.Errors:
case err, ok := <-f.w.Errors:
if !ok {
cb(nil, errors.New("fsnotify err channel closed"))
break loop
Expand All @@ -120,9 +126,14 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
}
}

w.Close()
f.w.Close()
}()

// Watch the directory for changes.
return w.Add(fDir)
return f.w.Add(fDir)
}

// Unwatch stops watching the files. It closes all the fsnotify watchers
// and event channels associated with them.
func (f *File) Unwatch() error {
return f.w.Close()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f.w = nil is missing here for the case of restart watching (there is checking for f.w != nil

}
33 changes: 33 additions & 0 deletions tests/koanf_test.go
Expand Up @@ -476,6 +476,39 @@ func TestWatchFile(t *testing.T) {
}, "file watch reload didn't change config")
}

func TestUnwatchFile(t *testing.T) {
var (
assert = assert.New(t)
k = koanf.New(delim)
)

// Create a tmp config file.
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "koanf_mock")
err := os.WriteFile(tmpFile, []byte(`{"parent": {"name": "name1"}}`), 0600)
require.NoError(t, err, "error creating temp config file: %v", err)

// Load the new config file.
f := file.Provider(tmpFile)
k.Load(f, json.Parser())

var wg sync.WaitGroup
wg.Add(1) // our assurance that cb is called max once
f.Watch(func(event interface{}, err error) {
expectedErrorMessage := "fsnotify watch channel closed"
// When the file is changed, and the fsnotify watch channel is closed, an error is thrown. Assert this condition.
assert.EqualError(err, expectedErrorMessage, "Expected error message: %s", expectedErrorMessage)
wg.Done()
})

// unwatch the file so that it is not reloaded when changed
f.Unwatch()
// Wait a second and change the file.
time.Sleep(1 * time.Second)
os.WriteFile(tmpFile, []byte(`{"parent": {"name": "name2"}}`), 0600)
wg.Wait()
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sequence of Watch/Unwatch/Watch needs to be checked

func TestWatchFileSymlink(t *testing.T) {
var (
assert = assert.New(t)
Expand Down