[#418] improved retry logic to prevent loading loop (#427)

* [#418] improved retry logic to prevent loading loop

* [#418] added FullJitter algorithm

Co-authored-by: Benoit TELLIER <btellier@linagora.com>
This commit is contained in:
Camille Moussu
2025-12-19 12:24:34 +01:00
committed by GitHub
parent 90d6058b0f
commit 6839353f34
5 changed files with 70 additions and 9 deletions
+34 -2
View File
@@ -1,8 +1,26 @@
import ky from "ky";
import { Auth } from "../features/User/oidcAuth";
const RETRY_CONFIG = {
maxRetries: 10,
initialDelay: 1000,
maxDelay: 120000,
};
function getRetryDelay(attemptNumber: number): number {
const cap = RETRY_CONFIG.maxDelay;
const base = RETRY_CONFIG.initialDelay * Math.pow(2, attemptNumber);
const jitter = 0.5 + Math.random();
return Math.min(cap, base * jitter);
}
export const api = ky.extend({
prefixUrl: (window as any).CALENDAR_BASE_URL,
retry: {
limit: RETRY_CONFIG.maxRetries,
backoffLimit: RETRY_CONFIG.maxDelay,
delay: (attemptCount) => getRetryDelay(attemptCount - 1),
},
hooks: {
beforeRequest: [
async (request) => {
@@ -10,11 +28,25 @@ export const api = ky.extend({
? JSON.parse(sessionStorage.getItem("tokenSet")!)
: null;
const access_token = saved?.access_token;
request.headers.set("Authorization", `Bearer ${access_token}`);
if (access_token) {
request.headers.set("Authorization", `Bearer ${access_token}`);
}
return request;
},
],
beforeRetry: [
async ({ request, error, retryCount }) => {
console.warn(
`[API Retry] Attempt ${retryCount}/${RETRY_CONFIG.maxRetries}`,
{
url: request.url,
error: error?.message,
}
);
},
],
afterResponse: [
async (request, options, response) => {
if (response.status === 401) {