Skip to content

FFM-9097 Don't set global Axios timeout #114

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 14 commits into from
May 2, 2024
Merged
1 change: 1 addition & 0 deletions docs/further_reading.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ enableAnalytics: boolean; // enable analytics
cache: KeyValueStore; // set custom cache (default lru cache)
store: AsyncKeyValueStore; // set custom persistent store (default file store)
logger: Logger; // set logger (default console)
axiosTimeout: number; // set timeout for requests to Harness (default 30s)
```

## Singleton example
Expand Down
49 changes: 32 additions & 17 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from 'events';
import jwt_decode from 'jwt-decode';
import axios, { AxiosInstance } from 'axios';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import axiosRetry from 'axios-retry';
import { Claims, Options, StreamEvent, Target } from './types';
import { Configuration, ClientApi, FeatureConfig, Variation } from './openapi';
Expand Down Expand Up @@ -109,22 +109,13 @@ export default class Client {
);
this.evaluator = new Evaluator(this.repository, this.log);

if (options.tlsTrustedCa) {
this.httpsCa = fs.readFileSync(options.tlsTrustedCa);
this.httpsClient = axios.create({
httpsAgent: new https.Agent({
ca: this.httpsCa,
}),
});

this.api = new ClientApi(
this.configuration,
this.options.baseUrl,
this.httpsClient,
);
} else {
this.api = new ClientApi(this.configuration);
}
// Setup https client for sass or on-prem connections
this.httpsClient = this.createAxiosInstanceWithRetries(options);
this.api = new ClientApi(
this.configuration,
this.options.baseUrl,
this.httpsClient,
);

this.processEvents();
this.run();
Expand Down Expand Up @@ -297,6 +288,30 @@ export default class Client {
return this.waitForInitializePromise;
}

private createAxiosInstanceWithRetries(options: Options): AxiosInstance {
let axiosConfig: AxiosRequestConfig = {
timeout: options.axiosTimeout,
};

if (options.tlsTrustedCa) {
const httpsCa = fs.readFileSync(options.tlsTrustedCa);
// Expanding axiosConfig with httpsAgent when TLS config is provided
axiosConfig = {
...axiosConfig,
httpsAgent: new https.Agent({
ca: httpsCa,
}),
};
}

const instance = axios.create(axiosConfig);
axiosRetry(instance, {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
});
return instance;
}

private initialize(processor: Processor): void {
switch (processor) {
case Processor.POLL:
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export const defaultOptions: Options = {
cache: new LRU({ max: 100 }),
store: new FileStore(),
logger: new ConsoleLog(),
axiosTimeout: 3000,
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Options {
store?: AsyncKeyValueStore;
logger?: Logger;
tlsTrustedCa?: string;
axiosTimeout?: number;
}

export interface Claims {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '1.7.0';
export const VERSION = '1.8.0';
Copy link
Contributor Author

@erdirowlands erdirowlands May 2, 2024

Choose a reason for hiding this comment

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

The version was bumped to 1.8.0 in the last PR and has not been released yet. We forgot to update the version here in that PR.
Therefore I haven't also bumped the minor version in this PR in package.json