Skip to content

feat: Allow deactivating polling by setting a negative polling #3205

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

Merged
merged 3 commits into from
Mar 13, 2025
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
11 changes: 5 additions & 6 deletions feature_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func New(config Config) (*GoFeatureFlag, error) {
case config.PollingInterval == 0:
// The default value for the poll interval is 60 seconds
config.PollingInterval = 60 * time.Second
case config.PollingInterval < 0:
// Check that value is not negative
return nil, fmt.Errorf("%d is not a valid PollingInterval value, it need to be > 0", config.PollingInterval)
case config.PollingInterval < time.Second:
case config.PollingInterval > 0 && config.PollingInterval < time.Second:
// the minimum value for the polling policy is 1 second
config.PollingInterval = time.Second
default:
Expand All @@ -89,7 +86,6 @@ func New(config Config) (*GoFeatureFlag, error) {
notifiers = append(notifiers, &logsnotifier.Notifier{Logger: config.internalLogger})

notificationService := cache.NewNotificationService(notifiers)
goFF.bgUpdater = newBackgroundUpdater(config.PollingInterval, config.EnablePollingJitter)
goFF.cache = cache.New(notificationService, config.PersistentFlagConfigurationFile, config.internalLogger)

retrievers, err := config.GetRetrievers()
Expand Down Expand Up @@ -120,7 +116,10 @@ func New(config Config) (*GoFeatureFlag, error) {
}
}

go goFF.startFlagUpdaterDaemon()
if config.PollingInterval > 0 {
goFF.bgUpdater = newBackgroundUpdater(config.PollingInterval, config.EnablePollingJitter)
go goFF.startFlagUpdaterDaemon()
}

exporters := goFF.config.GetDataExporters()
if len(exporters) > 0 {
Expand Down
45 changes: 36 additions & 9 deletions feature_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ func TestMultipleRetrieversWithOverrideFlag(t *testing.T) {
assert.NotEqual(t, flag.ErrorCodeFlagNotFound, flagRes2.ErrorCode)
}

func TestStartWithNegativeInterval(t *testing.T) {
_, err := ffclient.New(ffclient.Config{
PollingInterval: -60 * time.Second,
Retriever: &fileretriever.Retriever{Path: "testdata/flag-config.yaml"},
LeveledLogger: slog.Default(),
})
assert.Error(t, err)
}

func TestStartWithMinInterval(t *testing.T) {
_, err := ffclient.New(ffclient.Config{
PollingInterval: 2,
Expand Down Expand Up @@ -743,3 +734,39 @@ func Test_DisableNotifierOnInit(t *testing.T) {
})
}
}

func TestStartWithNegativeIntervalToDisablePolling(t *testing.T) {
content, err := os.ReadFile("testdata/flag-config.yaml")
assert.NoError(t, err)

// copy of the file
tempFile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { _ = os.Remove(tempFile.Name()) }()
err = os.WriteFile(tempFile.Name(), content, os.ModePerm)
assert.NoError(t, err)

goff, err := ffclient.New(ffclient.Config{
PollingInterval: -1 * time.Second,
Retriever: &fileretriever.Retriever{Path: tempFile.Name()},
LeveledLogger: slog.Default(),
})
assert.NoError(t, err)

cacheRefresh := goff.GetCacheRefreshDate()

// modify the file to trigger a refresh
newContent, err := os.ReadFile("testdata/flag-config-2nd-file.yaml")
assert.NoError(t, err)
err = os.WriteFile(tempFile.Name(), newContent, os.ModePerm)
assert.NoError(t, err)

// wait to be sure we give time to the goroutine to refresh the cache
time.Sleep(2 * time.Second)

assert.Equal(t, cacheRefresh, goff.GetCacheRefreshDate())

// we force a refresh to check if the cache is refreshed
goff.ForceRefresh()
assert.NotEqual(t, cacheRefresh, goff.GetCacheRefreshDate())
}
Loading