Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Mar 14, 2024
2 parents 81865b0 + f35c554 commit 3e76300
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
6 changes: 5 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ type Config struct {
// timer triggers.
Interval time.Duration

// Interval at which to fetch new feature flags, 5min by default
// Interval at which to fetch new feature flag definitions, 5min by default
DefaultFeatureFlagsPollingInterval time.Duration

// Timeout for fetching feature flags, 3 seconds by default
FeatureFlagRequestTimeout time.Duration

// Calculate when feature flag definitions 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
26 changes: 21 additions & 5 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 @@ -35,6 +34,7 @@ type FeatureFlagsPoller struct {
http http.Client
mutex sync.RWMutex
fetchedFlagsSuccessfullyOnce bool
nextPollTick func() time.Duration
flagTimeout time.Duration
}

Expand Down Expand Up @@ -115,9 +115,22 @@ 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, flagTimeout 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,
flagTimeout 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 @@ -128,6 +141,7 @@ func newFeatureFlagsPoller(projectApiKey string, personalApiKey string, errorf f
http: httpClient,
mutex: sync.RWMutex{},
fetchedFlagsSuccessfullyOnce: false,
nextPollTick: nextPollTick,
flagTimeout: flagTimeout,
}

Expand All @@ -139,16 +153,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
11 changes: 10 additions & 1 deletion posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ 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.FeatureFlagRequestTimeout)
c.featureFlagsPoller = newFeatureFlagsPoller(
c.key,
c.Config.PersonalApiKey,
c.Errorf,
c.Endpoint,
c.http,
c.DefaultFeatureFlagsPollingInterval,
c.NextFeatureFlagsPollingTick,
c.FeatureFlagRequestTimeout,
)
}

go c.loop()
Expand Down

0 comments on commit 3e76300

Please sign in to comment.