set limit api call count

This commit is contained in:
hyunwoo93.cha
2024-07-08 16:01:06 +09:00
parent 5750aa09a6
commit 4e81c332b5

View File

@@ -6,6 +6,7 @@ import { types } from "../actions/actionTypes";
import {
changeAppStatus,
changeLocalSettings,
loadingComplete,
setShowPopup,
setSystemNotice,
setSystemTermination,
@@ -18,6 +19,8 @@ import { ACTIVE_POPUP } from "../utils/Config";
import * as HelperMethods from "../utils/helperMethods";
import { getUrl, URLS } from "./apiConfig";
const MAX_RETRIES = 10;
const INITIAL_DELAY = 100;
let tokenRefreshing = false;
export const setTokenRefreshing = (value) => {
console.log("TAxios setTokenRefreshing ", value);
@@ -35,7 +38,7 @@ export const TAxios = (
onFail,
noTokenRefresh = false
) => {
const checkAccessToken = () => {
const checkAccessToken = (retryCount = 0) => {
return new Promise((resolve) => {
const accessToken = getState().localSettings.accessToken;
if (noTokenRefresh || (!tokenRefreshing && accessToken)) {
@@ -44,14 +47,21 @@ export const TAxios = (
if (!accessToken && !tokenRefreshing) {
dispatch(getAuthenticationCode());
}
HelperMethods.wait(100).then(() => {
resolve(checkAccessToken());
const delay = INITIAL_DELAY * Math.pow(2, retryCount);
HelperMethods.wait(delay).then(() => {
if (retryCount < MAX_RETRIES) {
resolve(checkAccessToken(retryCount + 1));
} else {
console.error("max retries reached for token refresh");
onFail(new Error("max retries reached for token refresh"));
resolve(null);
}
});
}
});
};
const executeRequest = (accessToken) => {
const executeRequest = (accessToken, retryCount) => {
const httpHeader = getState().common.httpHeader;
const { mbr_no, deviceId } = getState().common.appStatus;
const refreshToken = getState().localSettings.refreshToken;
@@ -107,6 +117,8 @@ export const TAxios = (
if (baseUrl === URLS.GET_AUTHENTICATION_CODE) {
if (res?.data?.retCode !== 0) {
console.error("accessToken failed", res.data.retCode);
dispatch(loadingComplete(false));
dispatch(changeAppStatus({ isAuthenticationComplete: false }));
dispatch(
setShowPopup(ACTIVE_POPUP.networkErrorPopup, {
data: res.data.retCode,
@@ -129,19 +141,25 @@ export const TAxios = (
dispatch(getReAuthenticationCode());
}
checkAccessToken().then((token) => {
executeRequest(token);
executeRequest(token, retryCount);
});
return;
}
//RefreshToken 만료
if (res?.data?.retCode === 402 || res?.data?.retCode === 501) {
if (!tokenRefreshing) {
dispatch(getAuthenticationCode());
if (retryCount < MAX_RETRIES) {
if (!tokenRefreshing) {
dispatch(getAuthenticationCode());
}
const delay = INITIAL_DELAY * Math.pow(2, retryCount);
setTimeout(() => {
checkAccessToken().then((token) => {
executeRequest(token, retryCount + 1);
});
}, delay);
return;
}
checkAccessToken().then((token) => {
executeRequest(token);
});
return;
}
// 602 요청 국가 불일치
if (res?.data?.retCode === 602) {