Skip to content

[MAINT] retry mechanism using axios-retry #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2021
Merged
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
4 changes: 4 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ const { Client } = pkg;

const client = new Client('1c100d25-4c3f-487b-b198-3b3d01df5794');

console.log('Starting application');

setInterval(() => {
const target = {
identifier: 'harness',
};
const value = client.boolVariation('test', target, false);
console.log('Evaluation for flag test and target: ', value, target);
}, 10000);

console.log('Application started');
14 changes: 14 additions & 0 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"axios-retry": "^3.2.0",
"eventsource": "^1.1.0",
"jwt-decode": "^3.1.2",
"keyv": "^4.0.3",
Expand Down
55 changes: 37 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as events from 'events';
import jwt_decode from 'jwt-decode';
import axios from 'axios';
import axiosRetry from 'axios-retry';
import { Claims, Options, Target } from './types';
import { Configuration, ClientApi, FeatureConfig, Variation } from './openapi';
import { VERSION } from './version';
Expand All @@ -10,6 +12,8 @@ import { defaultOptions } from './constants';
import { Repository, StorageRepository } from './repository';
import { MetricsProcessor, MetricsProcessorInterface } from './metrics';

axios.defaults.timeout = 30000;
axiosRetry(axios, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
const log = defaultOptions.logger;

export default class Client {
Expand All @@ -29,6 +33,10 @@ export default class Client {

constructor(sdkKey: string, options: Options = {}) {
this.sdkKey = sdkKey;
if (options.pollInterval < 1000) {
options.pollInterval = defaultOptions.pollInterval;
log.warn('Polling interval cannot be lower than 1000 ms');
}
this.options = { ...defaultOptions, ...options };

this.configuration = new Configuration({
Expand All @@ -45,27 +53,30 @@ export default class Client {
this.options.store,
);
this.evaluator = new Evaluator(this.repository);

this.api = new ClientApi(this.configuration);
this.run();
}

private async authenticate(): Promise<string> {
const response = await this.api.authenticate({
apiKey: this.sdkKey,
});
this.authToken = response.data.authToken;
this.configuration.accessToken = this.authToken;
private async authenticate(): Promise<void> {
try {
const response = await this.api.authenticate({
apiKey: this.sdkKey,
});
this.authToken = response.data.authToken;
this.configuration.accessToken = this.authToken;

const decoded: Claims = jwt_decode(this.authToken);
const decoded: Claims = jwt_decode(this.authToken);

this.environment = decoded.environment;
this.cluster = decoded.clusterIdentifier || '1';
return this.authToken;
this.environment = decoded.environment;
this.cluster = decoded.clusterIdentifier || '1';
} catch (error) {
console.error('Error while authenticating, err: ', error);
}
}

private async run(): Promise<void> {
await this.authenticate();

this.pollProcessor = new PollingProcessor(
this.environment,
this.cluster,
Expand Down Expand Up @@ -114,7 +125,9 @@ export default class Client {
target,
defaultValue,
(fc: FeatureConfig, target: Target, variation: Variation) => {
this.metricsProcessor.enqueue(target, fc, variation);
if (this.metricsProcessor) {
this.metricsProcessor.enqueue(target, fc, variation);
}
},
);
}
Expand All @@ -129,7 +142,9 @@ export default class Client {
target,
defaultValue,
(fc: FeatureConfig, target: Target, variation: Variation) => {
this.metricsProcessor.enqueue(target, fc, variation);
if (this.metricsProcessor) {
this.metricsProcessor.enqueue(target, fc, variation);
}
},
);
}
Expand All @@ -144,7 +159,9 @@ export default class Client {
target,
defaultValue,
(fc: FeatureConfig, target: Target, variation: Variation) => {
this.metricsProcessor.enqueue(target, fc, variation);
if (this.metricsProcessor) {
this.metricsProcessor.enqueue(target, fc, variation);
}
},
);
}
Expand All @@ -159,18 +176,20 @@ export default class Client {
target,
defaultValue,
(fc: FeatureConfig, target: Target, variation: Variation) => {
this.metricsProcessor.enqueue(target, fc, variation);
if (this.metricsProcessor) {
this.metricsProcessor.enqueue(target, fc, variation);
}
},
);
}

close(): void {
this.pollProcessor.close();
if (this.options.enableStream) {
if (this.streamProcessor) {
this.streamProcessor.close();
}
if (this.options.enableAnalytics) {
if (this.metricsProcessor) {
this.metricsProcessor.close();
}
}
}
}
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ export const BASE_URL = 'https://config.ff.harness.io/api/1.0',
SECOND = 1000,
MINUTE = 60 * SECOND,
PULL_INTERVAL = 1 * MINUTE,
PERSIST_INTERVAL = 1 * MINUTE,
EVENTS_SYNC_INTERVAL = 1 * MINUTE;

export const defaultOptions: Options = {
baseUrl: BASE_URL,
eventsUrl: EVENTS_URL,
pollInterval: PULL_INTERVAL,
persistInterval: PERSIST_INTERVAL,
eventsSyncInterval: EVENTS_SYNC_INTERVAL,
enableStream: true,
enableAnalytics: true,
Expand Down
16 changes: 8 additions & 8 deletions src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PollingProcessor {
private environment: string;
private cluster: string;
private api: ClientApi;
private stopped = false;
private stopped = true;
private options: Options;
private repository: Repository

Expand All @@ -35,7 +35,7 @@ export class PollingProcessor {
});

eventBus.on(Event.DISCONNECTED, () => {
this.resume();
this.start();
});
}

Expand Down Expand Up @@ -99,23 +99,23 @@ export class PollingProcessor {
}

start(): void {
if (!this.stopped) {
log.info('PollingProcessor already started');
return;
}
log.info(
'Starting PollingProcessor with request interval: ',
this.options.pollInterval,
);
this.stopped = false;
this.poll();
}

private stop(): void {
log.info('Pausing PollingProcessor');
log.info('Stopping PollingProcessor');
this.stopped = true;
}

resume(): void {
log.info('Resuming PollingProcessor');
this.stopped = false;
}

close(): void {
log.info('Closing PollingProcessor');
this.stop();
Expand Down
2 changes: 1 addition & 1 deletion src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class StreamProcessor {
return this.eventSource.readyState == StreamProcessor.CONNECTED;
}

private stop(): void {
stop(): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you'd want this to be public, but not the equivalent function in polling.ts? Just curious

Copy link
Contributor Author

@enver-bisevac enver-bisevac Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch :), I was doing a test and I needed to stop streaming service and then forgot to return to private modifier

log.info('Stopping StreamProcessor');
this.eventSource.close();
this.eventBus.emit(Event.DISCONNECTED);
Expand Down