Skip to content

Commit

Permalink
Added generate increment request id on retry
Browse files Browse the repository at this point in the history
  • Loading branch information
flops committed Aug 9, 2024
1 parent 1792dbc commit 2b951d6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/ui/libs/DatalensChartkit/modules/axios/axios.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {AxiosError} from 'axios';
import type {AxiosError, AxiosRequestConfig} from 'axios';
import axios from 'axios';
import axiosRetry, {isRetryableError} from 'axios-retry';
import isNumber from 'lodash/isNumber';
import {REQUEST_ID_HEADER} from 'shared';
import {sleep} from 'shared/modules';
import {showReadOnlyToast} from 'ui/utils/readOnly';

Expand All @@ -17,6 +18,22 @@ const client = axios.create({
xsrfCookieName: '',
});

export const axiosOnRetryHandler = (
retryCount: number,
_: AxiosError,
requestConfig: AxiosRequestConfig,
) => {
if (requestConfig?.headers?.[REQUEST_ID_HEADER]) {
let requestId = requestConfig.headers[REQUEST_ID_HEADER];

if (retryCount > 1) {
requestId = requestId.slice(0, requestId.lastIndexOf('.'));
}

requestConfig.headers[REQUEST_ID_HEADER] = `${requestId}.${retryCount}`;
}
};

initConcurrencyManager(Infinity);

export function initConcurrencyManager(maxConcurrentRequests: number) {
Expand Down Expand Up @@ -45,6 +62,7 @@ export function initConcurrencyManager(maxConcurrentRequests: number) {
retries: 0,
retryCondition: isRetryableError,
retryDelay: () => 3000,
onRetry: axiosOnRetryHandler,
});

// it is necessary to store the indexes of the positions of the interceptors added by axios-retry, to bring ability to delete them
Expand Down

0 comments on commit 2b951d6

Please sign in to comment.