Skip to content

Commit

Permalink
[native] Retry feature flags call
Browse files Browse the repository at this point in the history
Summary:
Retry feature flags call. If number of tries is exceeded, rethrow an error.

Depends on D8652

Test Plan: Add console logs and simulate failed requests - the requests should be retried up to `numberOfTries` times with delays.

Reviewers: kamil, bartek

Reviewed By: bartek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D8679
  • Loading branch information
palys-swm committed Aug 1, 2023
1 parent 3aee560 commit c700760
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions native/components/feature-flags-provider.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as React from 'react';
import { Platform } from 'react-native';

import { fetchFeatureFlags } from 'lib/utils/feature-flags-utils.js';
import sleep from 'lib/utils/sleep.js';
import { useIsCurrentUserStaff } from 'native/utils/staff-utils.js';

import { codeVersion } from '../redux/persist.js';
Expand Down Expand Up @@ -64,11 +65,12 @@ function FeatureFlagsProvider(props: Props): React.Node {
React.useEffect(() => {
(async () => {
try {
const config = await fetchFeatureFlags(
Platform.OS,
isStaff,
codeVersion,
const config = await tryMultipleTimes(
() => fetchFeatureFlags(Platform.OS, isStaff, codeVersion),
3,
5000,
);

const configuration = {};
for (const feature of config.enabledFeatures) {
configuration[feature] = true;
Expand All @@ -94,4 +96,24 @@ function FeatureFlagsProvider(props: Props): React.Node {
);
}

async function tryMultipleTimes<T>(
f: () => Promise<T>,
numberOfTries: number,
delay: number,
): Promise<T> {
let lastError;
while (numberOfTries > 0) {
try {
return await f();
} catch (e) {
--numberOfTries;
lastError = e;
if (numberOfTries > 0) {
await sleep(delay);
}
}
}
throw lastError;
}

export { FeatureFlagsContext, FeatureFlagsProvider, featureFlagsStorageKey };

0 comments on commit c700760

Please sign in to comment.