Skip to content
Merged
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
2 changes: 2 additions & 0 deletions ldoverrides/file_source_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/launchdarkly/go-server-sdk/v7/internal/filedata"
"github.com/launchdarkly/go-server-sdk/v7/ldfilewatch"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
)

Expand Down Expand Up @@ -174,5 +175,6 @@ func (b *FileSourceBuilder) Build(context subsystems.ClientContext) (subsystems.
changeDetection: b.changeDetection,
pollInterval: pollInterval,
loggers: loggers,
watchFiles: ldfilewatch.WatchOptionalFiles,
}, nil
}
16 changes: 12 additions & 4 deletions ldoverrides/file_source_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-server-sdk/v7/internal/filedata"
"github.com/launchdarkly/go-server-sdk/v7/ldfilewatch"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
"github.com/launchdarkly/go-server-sdk/v7/subsystems/ldstoreimpl"
"github.com/launchdarkly/go-server-sdk/v7/subsystems/ldstoretypes"
Expand All @@ -20,6 +19,8 @@ type fileOverrideSource struct {
changeDetection ChangeDetection
pollInterval time.Duration
loggers ldlog.Loggers
// watchFiles sets up watching mode. Tests replace it with one that fails.
watchFiles func(paths []string, loggers ldlog.Loggers, reload func(), closeCh <-chan struct{}) error

reloader *filedata.Reloader
poller *filedata.Poller
Expand Down Expand Up @@ -61,9 +62,16 @@ func (f *fileOverrideSource) Start(sink subsystems.OverrideSink) {
switch f.changeDetection {
case Watching:
f.closeWatchCh = make(chan struct{})
if err := ldfilewatch.WatchOptionalFiles(f.paths, f.loggers, f.reloader.Trigger, f.closeWatchCh); err != nil {
// COVERAGE: constructing a watcher only fails under unusual OS conditions
f.loggers.Errorf("Unable to watch override files: %s", err)
if err := f.watchFiles(f.paths, f.loggers, f.reloader.Trigger, f.closeWatchCh); err != nil {
// The watcher itself cannot be created, for example because the limit on inotify
// instances is reached. Without change detection a later edit would never take effect,
// so the source polls the files instead, no more often than polling mode allows.
interval := f.pollInterval
if interval < MinimumPollInterval {
interval = MinimumPollInterval
}
f.loggers.Errorf("Unable to watch override files; polling them instead every %s: %s", interval, err)
f.poller = filedata.NewPoller(f.paths, interval, f.reloader.Trigger)
}
case Polling:
f.poller = filedata.NewPoller(f.paths, f.pollInterval, f.reloader.Trigger)
Expand Down
28 changes: 28 additions & 0 deletions ldoverrides/file_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ldoverrides

import (
"errors"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"os"
Expand Down Expand Up @@ -323,6 +324,33 @@ func TestFileSourceWatchingModeReloadsOnChange(t *testing.T) {
require.Len(t, flags, 0)
}

func TestFileSourceWatchingModeFallsBackToPollingWhenTheWatcherCannotBeCreated(t *testing.T) {
// Without change detection a later edit would never take effect. The source polls instead, at
// no less than the minimum poll interval, which watching mode otherwise ignores.
path := filepath.Join(t.TempDir(), "overrides.json")
writeFile(t, path, `{"flagValues": {"flag1": true}}`)

mockLog := ldlogtest.NewMockLog()
context := sharedtest.NewTestContext("", nil, &subsystems.LoggingConfiguration{Loggers: mockLog.Loggers})
source, err := FileSource().FilePaths(path).ChangeDetection(Watching).PollInterval(time.Millisecond).Build(context)
require.NoError(t, err)
source.(*fileOverrideSource).watchFiles = func([]string, ldlog.Loggers, func(), <-chan struct{}) error {
return errors.New("watcher unavailable")
}
sink := newCapturingSink()
source.Start(sink)
t.Cleanup(func() { _ = source.Close() })
sink.requireSnapshot(t)

writeFile(t, path, `{"flagValues": {"flag1": true, "flag2": false}}`)
newTime := time.Now().Add(2 * time.Second)
require.NoError(t, os.Chtimes(path, newTime, newTime))
flags := flagsByKey(t, sink.requireSnapshot(t))
require.Len(t, flags, 2)
mockLog.AssertMessageMatch(t, true, ldlog.Error,
"Unable to watch override files; polling them instead every 1s: watcher unavailable")
}

func TestFileSourcePollingModeReloadsOnChange(t *testing.T) {
path := filepath.Join(t.TempDir(), "overrides.json")
writeFile(t, path, `{"flagValues": {"flag1": true}}`)
Expand Down
Loading