Skip to content

Commit 3868a94

Browse files
author
FalkWolsky
committed
Network Retry mechanism for Flow API
1 parent b40c81a commit 3868a94

File tree

4 files changed

+8347
-10973
lines changed

4 files changed

+8347
-10973
lines changed

client/packages/lowcoder/src/api/subscriptionApi.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Api from "api/api";
2-
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
2+
import axios, { AxiosInstance, AxiosRequestConfig, CancelToken } from "axios";
33
import { useDispatch, useSelector } from "react-redux";
44
import { useEffect, useState} from "react";
55
import { calculateFlowCode } from "./apiUtils";
@@ -45,15 +45,53 @@ const getAxiosInstance = (clientSecret?: string) => {
4545
class SubscriptionApi extends Api {
4646
static async secureRequest(body: any): Promise<any> {
4747
let response;
48+
const axiosInstance = getAxiosInstance();
49+
50+
// Create a cancel token and set timeout for cancellation
51+
const source = axios.CancelToken.source();
52+
const timeoutId = setTimeout(() => {
53+
source.cancel("Request timed out.");
54+
}, 5000);
55+
56+
// Request configuration with cancel token
57+
const requestConfig: AxiosRequestConfig = {
58+
method: "POST",
59+
withCredentials: true,
60+
data: body,
61+
cancelToken: source.token, // Add cancel token
62+
};
63+
4864
try {
49-
response = await getAxiosInstance().request({
50-
method: "POST",
51-
withCredentials: true,
52-
data: body,
53-
});
65+
response = await axiosInstance.request(requestConfig);
5466
} catch (error) {
55-
console.error("Error at Secure Flow Request:", error);
67+
if (axios.isCancel(error)) {
68+
console.warn("Request cancelled due to timeout:", error.message);
69+
// Retry once after timeout cancellation
70+
try {
71+
// Reset the cancel token and retry
72+
const retrySource = axios.CancelToken.source();
73+
const retryTimeoutId = setTimeout(() => {
74+
retrySource.cancel("Retry request timed out.");
75+
}, 10000);
76+
77+
response = await axiosInstance.request({
78+
...requestConfig,
79+
cancelToken: retrySource.token,
80+
});
81+
82+
clearTimeout(retryTimeoutId);
83+
} catch (retryError) {
84+
console.error("Retry failed:", retryError);
85+
throw retryError;
86+
}
87+
} else {
88+
console.error("Error at Secure Flow Request:", error);
89+
throw error;
90+
}
91+
} finally {
92+
clearTimeout(timeoutId); // Clear the initial timeout
5693
}
94+
5795
return response;
5896
}
5997
}

client/packages/lowcoder/src/api/supportApi.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Api from "api/api";
2-
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
2+
import axios, { AxiosInstance, AxiosRequestConfig, CancelToken } from "axios";
33
import { calculateFlowCode } from "./apiUtils";
44

55
export type ResponseType = {
@@ -64,15 +64,53 @@ class SupportApi extends Api {
6464

6565
static async secureRequest(body: any): Promise<any> {
6666
let response;
67+
const axiosInstance = getAxiosInstance();
68+
69+
// Create a cancel token and set timeout for cancellation
70+
const source = axios.CancelToken.source();
71+
const timeoutId = setTimeout(() => {
72+
source.cancel("Request timed out.");
73+
}, 2000);
74+
75+
// Request configuration with cancel token
76+
const requestConfig: AxiosRequestConfig = {
77+
method: "POST",
78+
withCredentials: true,
79+
data: body,
80+
cancelToken: source.token, // Add cancel token
81+
};
82+
6783
try {
68-
response = await getAxiosInstance().request({
69-
method: "POST",
70-
withCredentials: true,
71-
data: body,
72-
});
84+
response = await axiosInstance.request(requestConfig);
7385
} catch (error) {
74-
console.error("Error at Support Flow Request:", error);
86+
if (axios.isCancel(error)) {
87+
console.warn("Request cancelled due to timeout:", error.message);
88+
// Retry once after timeout cancellation
89+
try {
90+
// Reset the cancel token and retry
91+
const retrySource = axios.CancelToken.source();
92+
const retryTimeoutId = setTimeout(() => {
93+
retrySource.cancel("Retry request timed out.");
94+
}, 2000);
95+
96+
response = await axiosInstance.request({
97+
...requestConfig,
98+
cancelToken: retrySource.token,
99+
});
100+
101+
clearTimeout(retryTimeoutId);
102+
} catch (retryError) {
103+
console.error("Retry failed:", retryError);
104+
throw retryError;
105+
}
106+
} else {
107+
console.error("Error at Support Flow Request:", error);
108+
throw error;
109+
}
110+
} finally {
111+
clearTimeout(timeoutId); // Clear the initial timeout
75112
}
113+
76114
return response;
77115
}
78116

client/packages/lowcoder/src/pages/support/supportDetail.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ export function SupportDetail() {
280280
if (ticketData && ticketData.length === 1) {
281281
setTicket(ticketData[0]);
282282

283-
console.log("Description", ticketData[0].fields.description);
284-
285283
setNewDescription(convertJiraToHtml(ticketData[0].fields.description) || '');
286284
} else {
287285
setError(trans("support.ticketNotFound"));

0 commit comments

Comments
 (0)