Skip to content

Commit

Permalink
feat: max retries config. (#896)
Browse files Browse the repository at this point in the history
* feat: max retries config.
  • Loading branch information
b4rtaz authored Dec 13, 2022
1 parent 2421101 commit 439d6e5
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 125 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-suns-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/common-core': patch
---

Added the `maxRetries` parameter to the Core config.
41 changes: 0 additions & 41 deletions demos/cli/src/EvmApi.ts

This file was deleted.

49 changes: 0 additions & 49 deletions demos/cli/src/SolApi.ts

This file was deleted.

27 changes: 0 additions & 27 deletions demos/cli/src/Tester.ts

This file was deleted.

2 changes: 1 addition & 1 deletion demos/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function readEnv(): { [key: string]: string } {

async function main() {
const env = readEnv();
console.info('🔥 test-node');
console.info('🔥 CLI demo');

Moralis.start({
apiKey: env['MORALIS_API_KEY'],
Expand Down
1 change: 1 addition & 0 deletions packages/common/core/src/Config/CoreConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ describe('CoreConfig', () => {
testSetAndGet<LogLevel>(CoreConfig.logLevel, 'off');
testSetAndGet<BuildEnvironment>(CoreConfig.buidEnvironment, 'react-native');
testSetAndGet<Network>(CoreConfig.defaultNetwork, 'Solana');
testSetAndGet<number>(CoreConfig.maxRetries, 5);
});
8 changes: 8 additions & 0 deletions packages/common/core/src/Config/CoreConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export const CoreConfig = {
name: 'product',
defaultValue: undefined,
} as ConfigKey<string | undefined>,

/**
* @description Maximal number of request retries.
*/
maxRetries: {
name: 'maxRetries',
defaultValue: 2,
} as ConfigKey<number>,
};
1 change: 1 addition & 0 deletions packages/common/core/src/Config/CoreConfigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export class CoreConfigSetup {

config.registerKey(CoreConfig.defaultNetwork);
config.registerKey(CoreConfig.product);
config.registerKey(CoreConfig.maxRetries);
}
}
4 changes: 2 additions & 2 deletions packages/common/core/src/controllers/AxiosRetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ describe('AxiosRetry', () => {
});
}

function createRetryConfig(maxAttempts: number): AxiosRetryConfig {
function createRetryConfig(maxRetries: number): AxiosRetryConfig {
return {
maxAttempts,
maxRetries,
allowedMethods: ['GET'],
allowedResponseStatuses: [444],
};
Expand Down
4 changes: 2 additions & 2 deletions packages/common/core/src/controllers/AxiosRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isTest } from '../environment/isTest';
import { noop } from '../utils/noop';

export interface AxiosRetryConfig {
maxAttempts: number;
maxRetries: number;
allowedMethods: string[];
allowedResponseStatuses: number[];
beforeRetry?: (attempt: number, error: AxiosError) => void;
Expand All @@ -30,7 +30,7 @@ export class AxiosRetry {
const response = await axios.request(requestConfig);
return response;
} catch (e) {
if (attempt >= retryConfig.maxAttempts) {
if (attempt >= retryConfig.maxRetries) {
throw e;
}
if (!requestConfig.method || !retryConfig.allowedMethods.includes(requestConfig.method.toUpperCase())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AxiosRetry, AxiosRetryConfig } from '../AxiosRetry';
import { Core } from '../../Core';
import { LoggerController } from '../LoggerController';
import { getMessageFromApiRequestError, isApiRequestError } from './ApiRequestError';
import { Config, CoreConfig } from '../../Config';

export interface RequestOptions {
headers?: { [name: string]: string };
Expand All @@ -15,10 +16,10 @@ export interface RequestOptions {
*/
export class RequestController {
public static create(core: Core): RequestController {
return new RequestController(core.logger);
return new RequestController(core.config, core.logger);
}

private constructor(private readonly logger: LoggerController) {}
private constructor(private readonly config: Config, private readonly logger: LoggerController) {}

public async request<Data, Response>(config: AxiosRequestConfig<Data>): Promise<Response> {
this.logger.verbose('[RequestController] request started', {
Expand All @@ -27,8 +28,9 @@ export class RequestController {
body: config.data,
});

const maxRetries = this.config.get(CoreConfig.maxRetries);
const retryConfig: AxiosRetryConfig = {
maxAttempts: 2,
maxRetries,
allowedMethods: ['GET', 'OPTIONS'],
allowedResponseStatuses: [408, 413, 429, 500, 502, 503, 504],
beforeRetry: (attempt: number, error: AxiosError) => {
Expand Down

1 comment on commit 439d6e5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 25%
26.34% (49/186) 19.14% (9/47) 22.85% (8/35)
auth Coverage: 90%
92.77% (77/83) 81.81% (18/22) 90% (18/20)
evm-api Coverage: 100%
100% (80/80) 66.66% (6/9) 100% (48/48)
common-evm-utils Coverage: 64%
64.99% (947/1457) 19.43% (123/633) 35.8% (203/567)
sol-api Coverage: 96%
96.66% (29/30) 66.66% (6/9) 91.66% (11/12)
common-sol-utils Coverage: 74%
73.77% (135/183) 60% (12/20) 65.67% (44/67)
common-streams-utils Coverage: 93%
93.13% (787/845) 85.96% (196/228) 84.14% (276/328)
streams Coverage: 87%
86.82% (402/463) 67.6% (48/71) 84.52% (71/84)

Please sign in to comment.