Skip to content
Closed
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eppo/js-client-sdk-common",
"version": "4.15.1",
"version": "4.16.0-experimental.1",
"description": "Common library for Eppo JavaScript SDKs (web, react native, and node)",
"main": "dist/index.js",
"files": [
Expand Down
4 changes: 3 additions & 1 deletion src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
} from '../interfaces';
import { getMD5Hash } from '../obfuscation';
import { OverridePayload, OverrideValidator } from '../override-validator';
import initPoller, { IPoller } from '../poller';
import initPoller, { IPoller, PollInterceptor } from '../poller';
import SdkTokenDecoder from '../sdk-token-decoder';
import {
Attributes,
Expand Down Expand Up @@ -89,6 +89,7 @@ export type FlagConfigurationRequestParameters = {
pollAfterFailedInitialization?: boolean;
throwOnFailedInitialization?: boolean;
skipInitialPoll?: boolean;
pollWrapper?: PollInterceptor;
};

export interface IContainerExperiment<T> {
Expand Down Expand Up @@ -355,6 +356,7 @@ export default class EppoClient {
pollAfterFailedStart: pollAfterFailedInitialization,
errorOnFailedStart: throwOnFailedInitialization,
skipInitialPoll: skipInitialPoll,
pollIntercept: this.configurationRequestParameters?.pollWrapper,
});

await this.requestPoller.start();
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
Environment,
} from './interfaces';
import { buildStorageKeySuffix } from './obfuscation';
import { PollInterceptor } from './poller';
import {
AttributeType,
Attributes,
Expand Down Expand Up @@ -96,6 +97,7 @@ export {
FlagConfigRequestor,
HttpClient,
validation,
PollInterceptor as PollWrapper,

// Precomputed Client
EppoPrecomputedClient,
Expand Down
68 changes: 40 additions & 28 deletions src/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface IPoller {
start: () => Promise<void>;
stop: () => void;
}
export type PollInterceptor = (pollAndProcess: () => Promise<void>) => Promise<void>;

const defaultPollInterceptor: PollInterceptor = async (pollAndProcess) => pollAndProcess();

// TODO: change this to a class with methods instead of something that returns a function

Expand All @@ -25,13 +28,15 @@ export default function initPoller(
errorOnFailedStart?: boolean;
pollAfterFailedStart?: boolean;
skipInitialPoll?: boolean;
pollIntercept?: PollInterceptor;
},
): IPoller {
let stopped = false;
let failedAttempts = 0;
let nextPollMs = intervalMs;
let previousPollFailed = false;
let nextTimer: NodeJS.Timeout | undefined = undefined;
const pollIntercept = options?.pollIntercept ?? defaultPollInterceptor;

const start = async () => {
stopped = false;
Expand Down Expand Up @@ -106,38 +111,45 @@ export default function initPoller(
if (stopped) {
return;
}
const workFunction = async () => {
try {
await callback();
// If no error, reset any retrying
failedAttempts = 0;
nextPollMs = intervalMs;
if (previousPollFailed) {
previousPollFailed = false;
logger.info('Eppo SDK poll successful; resuming normal polling');
}
} catch (error: any) {
previousPollFailed = true;
logger.warn(`Eppo SDK encountered an error polling configurations: ${error.message}`);
const maxTries = 1 + (options?.maxPollRetries ?? DEFAULT_POLL_CONFIG_REQUEST_RETRIES);
if (++failedAttempts < maxTries) {
const failureWaitMultiplier = Math.pow(2, failedAttempts);
const jitterMs = randomJitterMs(intervalMs);
nextPollMs = failureWaitMultiplier * intervalMs + jitterMs;
logger.warn(
`Eppo SDK will try polling again in ${nextPollMs} ms (${
maxTries - failedAttempts
} attempts remaining)`,
);
} else {
logger.error(
`Eppo SDK reached maximum of ${failedAttempts} failed polling attempts. Stopping polling`,
);
stop();
}
}
};

try {
await callback();
// If no error, reset any retrying
failedAttempts = 0;
nextPollMs = intervalMs;
if (previousPollFailed) {
previousPollFailed = false;
logger.info('Eppo SDK poll successful; resuming normal polling');
}
} catch (error: any) {
previousPollFailed = true;
logger.warn(`Eppo SDK encountered an error polling configurations: ${error.message}`);
const maxTries = 1 + (options?.maxPollRetries ?? DEFAULT_POLL_CONFIG_REQUEST_RETRIES);
if (++failedAttempts < maxTries) {
const failureWaitMultiplier = Math.pow(2, failedAttempts);
const jitterMs = randomJitterMs(intervalMs);
nextPollMs = failureWaitMultiplier * intervalMs + jitterMs;
logger.warn(
`Eppo SDK will try polling again in ${nextPollMs} ms (${
maxTries - failedAttempts
} attempts remaining)`,
);
} else {
logger.error(
`Eppo SDK reached maximum of ${failedAttempts} failed polling attempts. Stopping polling`,
);
stop();
}
await pollIntercept(workFunction);
} catch (e: any) {
logger.error(`Eppo SDK encountered an error with the polling wrapper: ${e.message}`);
}

setTimeout(poll, nextPollMs);
nextTimer = setTimeout(poll, nextPollMs);
}

return {
Expand Down
Loading