Skip to content

Commit

Permalink
feat: increase flag polling interval; add flag poller interval config
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Nov 24, 2024
1 parent 64cbec3 commit b5d064e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/experiment-browser/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ export interface ExperimentConfig {
*/
pollOnStart?: boolean;

/**
* The interval to poll local evaluation flag configurations on `start()`.
* Only used if `pollOnStart` is `true`. Minimum 60000.
*/
flagConfigPollingIntervalMillis?: number;

/**
* Explicitly enable or disable calling {@link fetch()} on {@link start()}:
*
Expand Down Expand Up @@ -159,6 +165,7 @@ export interface ExperimentConfig {
| **retryFailedAssignment** | `true` |
| **automaticExposureTracking** | `true` |
| **pollOnStart** | `true` |
| **flagConfigPollingIntervalMillis** | `300000` |
| **fetchOnStart** | `true` |
| **automaticFetchOnAmplitudeIdentityChange** | `false` |
| **userProvider** | `null` |
Expand All @@ -182,6 +189,7 @@ export const Defaults: ExperimentConfig = {
retryFetchOnFailure: true,
automaticExposureTracking: true,
pollOnStart: true,
flagConfigPollingIntervalMillis: 300000,
fetchOnStart: true,
automaticFetchOnAmplitudeIdentityChange: false,
userProvider: null,
Expand Down
18 changes: 13 additions & 5 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const fetchBackoffAttempts = 8;
const fetchBackoffMinMillis = 500;
const fetchBackoffMaxMillis = 10000;
const fetchBackoffScalar = 1.5;
const flagPollerIntervalMillis = 60000;
const minFlagPollerIntervalMillis = 60000;

const euServerUrl = 'https://api.lab.eu.amplitude.com';
const euFlagsServerUrl = 'https://flag.lab.eu.amplitude.com';
Expand All @@ -80,10 +80,7 @@ export class ExperimentClient implements Client {
private userProvider: ExperimentUserProvider | undefined;
private exposureTrackingProvider: ExposureTrackingProvider | undefined;
private retriesBackoff: Backoff | undefined;
private poller: Poller = new Poller(
() => this.doFlags(),
flagPollerIntervalMillis,
);
private poller: Poller;
private isRunning = false;
private readonly integrationManager: IntegrationManager;

Expand Down Expand Up @@ -116,7 +113,18 @@ export class ExperimentClient implements Client {
(config?.serverZone?.toLowerCase() === 'eu'
? euFlagsServerUrl
: Defaults.flagsServerUrl),
// Force minimum flag config polling interval.
flagConfigPollingIntervalMillis:
config.flagConfigPollingIntervalMillis < minFlagPollerIntervalMillis
? minFlagPollerIntervalMillis
: config.flagConfigPollingIntervalMillis
? config.flagConfigPollingIntervalMillis
: Defaults.flagConfigPollingIntervalMillis,
};
this.poller = new Poller(
() => this.doFlags(),
config.flagConfigPollingIntervalMillis,
);
// Transform initialVariants
if (this.config.initialVariants) {
for (const flagKey in this.config.initialVariants) {
Expand Down
20 changes: 20 additions & 0 deletions packages/experiment-browser/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnalyticsConnector } from '@amplitude/analytics-connector';
import { FetchError, safeGlobal } from '@amplitude/experiment-core';
import { Defaults } from 'src/config';
import { ExperimentEvent, IntegrationPlugin } from 'src/types/plugin';

import { version as PACKAGE_VERSION } from '../package.json';
Expand Down Expand Up @@ -1323,3 +1324,22 @@ describe('trackExposure variant metadata', () => {
expect(providerExposure).toBeUndefined();
});
});

describe('flag config polling interval config', () => {
test('undefined, set to default', () => {
const client = new ExperimentClient('api_key', {});
expect(client['config'].flagConfigPollingIntervalMillis).toEqual(300000);
});
test('defined, less than minimum, set to minimum', () => {
const client = new ExperimentClient('api_key', {
flagConfigPollingIntervalMillis: 1000,
});
expect(client['config'].flagConfigPollingIntervalMillis).toEqual(60000);
});
test('defined, greater than minimum, set to configured value', () => {
const client = new ExperimentClient('api_key', {
flagConfigPollingIntervalMillis: 900000,
});
expect(client['config'].flagConfigPollingIntervalMillis).toEqual(900000);
});
});

0 comments on commit b5d064e

Please sign in to comment.