Skip to content

Commit

Permalink
replaced axios-concurrency with build-in typescript version (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
melikhov-dev authored Apr 11, 2024
1 parent d003a8f commit 4b071b7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 22 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"ajv": "^8.12.0",
"aws4": "^1.11.0",
"axios": "^1.6.0",
"axios-concurrency": "^1.0.4",
"axios-retry": "^3.2.0",
"clipboard-copy": "^3.2.0",
"d3": "^7.8.5",
Expand Down
8 changes: 4 additions & 4 deletions src/ui/libs/DatalensChartkit/modules/axios/axios.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios, {AxiosError} from 'axios';
// @ts-ignore
import {ConcurrencyManager as concurrencyManager} from 'axios-concurrency';
import axiosRetry, {isRetryableError} from 'axios-retry';
import isNumber from 'lodash/isNumber';
import {showReadOnlyToast} from 'ui/utils/readOnly';

let concurrencyManagerInstance: typeof concurrencyManager = null;
import {ConcurrencyManagerInstance, concurrencyManager} from './axiosConcurrency';

let concurrencyManagerInstance: ConcurrencyManagerInstance;

const client = axios.create({
withCredentials: true,
Expand All @@ -17,7 +17,7 @@ const client = axios.create({
initConcurrencyManager(Infinity);

export function initConcurrencyManager(maxConcurrentRequests: number) {
if (concurrencyManagerInstance) {
if (concurrencyManagerInstance !== undefined) {
concurrencyManagerInstance.detach();
}

Expand Down
98 changes: 98 additions & 0 deletions src/ui/libs/DatalensChartkit/modules/axios/axiosConcurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// based on https://github.com/bernawil/axios-concurrency/

import {AxiosInstance, AxiosResponse, InternalAxiosRequestConfig} from 'axios';

type Handler = {
request: InternalAxiosRequestConfig;
resolver(value: unknown): void;
};

type Interceptors = {
request: number;
response: number;
};

export type ConcurrencyManagerInstance = {
queue: Handler[];
running: Handler[];
shiftInitial: () => void;
push: (reqHandler: Handler) => void;
shift: () => void;
requestHandler(req: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig>;
responseHandler(res: AxiosResponse): AxiosResponse;
responseErrorHandler(res: AxiosResponse): void;
interceptors: Interceptors;
detach(): void;
};

export const concurrencyManager = (
axios: AxiosInstance,
MAX_CONCURRENT = 10,
): ConcurrencyManagerInstance => {
if (MAX_CONCURRENT < 1) {
throw 'Concurrency Manager Error: minimun concurrent requests is 1';
}
const instance: ConcurrencyManagerInstance = {
queue: [],
running: [],
shiftInitial: () => {
setTimeout(() => {
if (instance.running.length < MAX_CONCURRENT) {
instance.shift();
}
}, 0);
},
push: (reqHandler) => {
instance.queue.push(reqHandler);
instance.shiftInitial();
},
shift: () => {
if (instance.queue.length) {
const queued = instance.queue.shift();
if (queued) {
if (queued.request.cancelToken && queued.request.cancelToken.reason) {
// the request was already cancelled - do not even start it, just forget it
instance.shift();
return;
}
queued.resolver(queued.request);
instance.running.push(queued);
}
}
},
// Use as interceptor. Queue outgoing requests
requestHandler: (req) => {
return new Promise((resolve) => {
instance.push({request: req, resolver: resolve});
});
},
// Use as interceptor. Execute queued request upon receiving a response
responseHandler: (res) => {
instance.running.shift();
instance.shift();
return res;
},
responseErrorHandler: (res) => {
return Promise.reject(instance.responseHandler(res));
},
interceptors: {
request: -1,
response: -1,
},
detach: () => {
if (instance.interceptors.request !== -1) {
axios.interceptors.request.eject(instance.interceptors.request);
}
if (instance.interceptors.response !== -1) {
axios.interceptors.response.eject(instance.interceptors.response);
}
},
};
// queue concurrent requests
instance.interceptors.request = axios.interceptors.request.use(instance.requestHandler);
instance.interceptors.response = axios.interceptors.response.use(
instance.responseHandler,
instance.responseErrorHandler,
);
return instance;
};

0 comments on commit 4b071b7

Please sign in to comment.