From 2b951d666154c87a0d91cecacfa49fe116604ff2 Mon Sep 17 00:00:00 2001 From: Serge Pavlyuk Date: Fri, 9 Aug 2024 23:02:55 +0300 Subject: [PATCH] Added generate increment request id on retry --- .../DatalensChartkit/modules/axios/axios.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ui/libs/DatalensChartkit/modules/axios/axios.ts b/src/ui/libs/DatalensChartkit/modules/axios/axios.ts index d14b12b201..b8c9af6c66 100644 --- a/src/ui/libs/DatalensChartkit/modules/axios/axios.ts +++ b/src/ui/libs/DatalensChartkit/modules/axios/axios.ts @@ -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'; @@ -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) { @@ -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