Skip to content

Commit 8b028d2

Browse files
committed
[FSSDK-11965] add retry for cmab fetch
1 parent 9c75ee1 commit 8b028d2

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

lib/client_factory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import { extractEventProcessor } from "./event_processor/event_processor_factory
2121
import { extractOdpManager } from "./odp/odp_manager_factory";
2222
import { extractVuidManager } from "./vuid/vuid_manager_factory";
2323
import { RequestHandler } from "./utils/http_request_handler/http";
24-
import { CLIENT_VERSION, DEFAULT_CMAB_CACHE_SIZE, DEFAULT_CMAB_CACHE_TIMEOUT, JAVASCRIPT_CLIENT_ENGINE } from "./utils/enums";
24+
import { CLIENT_VERSION, DEFAULT_CMAB_BACKOFF_MS, DEFAULT_CMAB_CACHE_SIZE, DEFAULT_CMAB_CACHE_TIMEOUT_MS, DEFAULT_CMAB_RETRIES, JAVASCRIPT_CLIENT_ENGINE } from "./utils/enums";
2525
import Optimizely from "./optimizely";
2626
import { DefaultCmabClient } from "./core/decision_service/cmab/cmab_client";
2727
import { CmabCacheValue, DefaultCmabService } from "./core/decision_service/cmab/cmab_service";
2828
import { InMemoryLruCache } from "./utils/cache/in_memory_lru_cache";
2929
import { transformCache, CacheWithRemove } from "./utils/cache/cache";
30+
import { ConstantBackoff, ExponentialBackoff } from "./utils/repeater/repeater";
3031

3132
export type OptimizelyFactoryConfig = Config & {
3233
requestHandler: RequestHandler;
@@ -53,13 +54,17 @@ export const getOptimizelyInstance = (config: OptimizelyFactoryConfig): Optimize
5354

5455
const cmabClient = new DefaultCmabClient({
5556
requestHandler,
57+
retryConfig: {
58+
maxRetries: DEFAULT_CMAB_RETRIES,
59+
backoffProvider: () => new ConstantBackoff(DEFAULT_CMAB_BACKOFF_MS),
60+
}
5661
});
5762

5863
const cmabCache: CacheWithRemove<CmabCacheValue> = config.cmab?.cache ?
5964
transformCache(config.cmab.cache, (value) => JSON.parse(value), (value) => JSON.stringify(value)) :
6065
(() => {
6166
const cacheSize = config.cmab?.cacheSize || DEFAULT_CMAB_CACHE_SIZE;
62-
const cacheTtl = config.cmab?.cacheTtl || DEFAULT_CMAB_CACHE_TIMEOUT;
67+
const cacheTtl = config.cmab?.cacheTtl || DEFAULT_CMAB_CACHE_TIMEOUT_MS;
6368
return new InMemoryLruCache<CmabCacheValue>(cacheSize, cacheTtl);
6469
})();
6570

lib/utils/enums/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,7 @@ export const DECISION_MESSAGES = {
102102
*/
103103
export const REQUEST_TIMEOUT_MS = 60 * 1000; // 1 minute
104104

105-
export const DEFAULT_CMAB_CACHE_TIMEOUT = 30 * 60 * 1000; // 30 minutes
105+
export const DEFAULT_CMAB_CACHE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
106106
export const DEFAULT_CMAB_CACHE_SIZE = 1000;
107+
export const DEFAULT_CMAB_RETRIES = 1;
108+
export const DEFAULT_CMAB_BACKOFF_MS = 100; // 100 milliseconds

lib/utils/repeater/repeater.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { expect, vi, it, beforeEach, afterEach, describe } from 'vitest';
17-
import { ExponentialBackoff, IntervalRepeater } from './repeater';
17+
import { ConstantBackoff, ExponentialBackoff, IntervalRepeater } from './repeater';
1818
import { advanceTimersByTime } from '../../tests/testUtils';
1919
import { resolvablePromise } from '../promise/resolvablePromise';
2020

@@ -88,6 +88,23 @@ describe("ExponentialBackoff", () => {
8888
});
8989

9090

91+
describe("ConstantBackoff", () => {
92+
it("should always return the same backoff time", () => {
93+
const constantBackoff = new ConstantBackoff(3000);
94+
for(let i = 0; i < 5; i++) {
95+
const time = constantBackoff.backoff();
96+
expect(time).toEqual(3000);
97+
}
98+
99+
// Reset to verify it still returns the same value
100+
constantBackoff.reset();
101+
for(let i = 0; i < 5; i++) {
102+
const time = constantBackoff.backoff();
103+
expect(time).toEqual(3000);
104+
}
105+
});
106+
});
107+
91108
describe("IntervalRepeater", () => {
92109
beforeEach(() => {
93110
vi.useFakeTimers();

lib/utils/repeater/repeater.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ export class ExponentialBackoff implements BackoffController {
6363
}
6464
}
6565

66+
export class ConstantBackoff implements BackoffController {
67+
private value: number;
68+
69+
constructor(value: number) {
70+
this.value = value;
71+
}
72+
73+
backoff(): number {
74+
return this.value;
75+
}
76+
77+
reset(): void {
78+
}
79+
}
80+
6681
// IntervalRepeater is a Repeater that invokes the task at a fixed interval
6782
// after the completion of the previous task invocation. If a backoff controller
6883
// is provided, the repeater will use the backoff controller to determine the

0 commit comments

Comments
 (0)