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

Allow to configure when next flags polling happens #36

Merged
merged 1 commit into from
Mar 14, 2024
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
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type Config struct {
// Interval at which to fetch new feature flags, 5min by default
DefaultFeatureFlagsPollingInterval time.Duration

// Calculate when feature flags should be polled next. Setting this property
// will override DefaultFeatureFlagsPollingInterval.
NextFeatureFlagsPollingTick func() time.Duration

// The HTTP transport used by the client, this allows an application to
// redefine how requests are being sent at the HTTP level (for example,
// to change the connection pooling policy).
Expand Down
25 changes: 20 additions & 5 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const LONG_SCALE = 0xfffffffffffffff

type FeatureFlagsPoller struct {
ticker *time.Ticker // periodic ticker
loaded chan bool
shutdown chan bool
forceReload chan bool
Expand All @@ -34,6 +33,7 @@ type FeatureFlagsPoller struct {
http http.Client
mutex sync.RWMutex
fetchedFlagsSuccessfullyOnce bool
nextPollTick func() time.Duration
}

type FeatureFlag struct {
Expand Down Expand Up @@ -113,9 +113,21 @@ func (e *InconclusiveMatchError) Error() string {
return e.msg
}

func newFeatureFlagsPoller(projectApiKey string, personalApiKey string, errorf func(format string, args ...interface{}), endpoint string, httpClient http.Client, pollingInterval time.Duration) *FeatureFlagsPoller {
func newFeatureFlagsPoller(
projectApiKey string,
personalApiKey string,
errorf func(format string, args ...interface{}),
endpoint string,
httpClient http.Client,
pollingInterval time.Duration,
nextPollTick func() time.Duration,
) *FeatureFlagsPoller {

if nextPollTick == nil {
nextPollTick = func() time.Duration { return pollingInterval }
}

poller := FeatureFlagsPoller{
ticker: time.NewTicker(pollingInterval),
loaded: make(chan bool),
shutdown: make(chan bool),
forceReload: make(chan bool),
Expand All @@ -126,6 +138,7 @@ func newFeatureFlagsPoller(projectApiKey string, personalApiKey string, errorf f
http: httpClient,
mutex: sync.RWMutex{},
fetchedFlagsSuccessfullyOnce: false,
nextPollTick: nextPollTick,
}

go poller.run()
Expand All @@ -136,16 +149,18 @@ func (poller *FeatureFlagsPoller) run() {
poller.fetchNewFeatureFlags()

for {
timer := time.NewTimer(poller.nextPollTick())
select {
case <-poller.shutdown:
close(poller.shutdown)
close(poller.forceReload)
close(poller.loaded)
poller.ticker.Stop()
timer.Stop()
return
case <-poller.forceReload:
timer.Stop()
poller.fetchNewFeatureFlags()
case <-poller.ticker.C:
case <-timer.C:
poller.fetchNewFeatureFlags()
}
}
Expand Down
10 changes: 9 additions & 1 deletion posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ func NewWithConfig(apiKey string, config Config) (cli Client, err error) {
}

if len(c.PersonalApiKey) > 0 {
c.featureFlagsPoller = newFeatureFlagsPoller(c.key, c.Config.PersonalApiKey, c.Errorf, c.Endpoint, c.http, c.DefaultFeatureFlagsPollingInterval)
c.featureFlagsPoller = newFeatureFlagsPoller(
c.key,
c.Config.PersonalApiKey,
c.Errorf,
c.Endpoint,
c.http,
c.DefaultFeatureFlagsPollingInterval,
c.NextFeatureFlagsPollingTick,
)
}

go c.loop()
Expand Down
Loading