Skip to content

Commit 078b393

Browse files
chore: update atoms retry mechanism (calcom#27450)
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
1 parent b5eda9c commit 078b393

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

packages/platform/atoms/cal-provider/CalProvider.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
"use client";
22

3+
import { VERSION_2024_06_14 } from "@calcom/platform-constants";
34
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5+
import { isAxiosError } from "axios";
46
import { useEffect } from "react";
5-
6-
import { VERSION_2024_06_14 } from "@calcom/platform-constants";
7-
87
import http from "../lib/http";
98
import type { CalProviderProps } from "./BaseCalProvider";
109
import { BaseCalProvider } from "./BaseCalProvider";
1110
import type { translationKeys } from "./languages";
1211

13-
const queryClient = new QueryClient();
12+
function isRetryableError(error: unknown): boolean {
13+
if (!isAxiosError(error) || !error.response) {
14+
// network errors or timeouts — retryable
15+
return true;
16+
}
17+
const status = error.response.status;
18+
// retry on 408 (timeout), 429 (rate limit), and 5xx (server errors)
19+
return status === 408 || status === 429 || status >= 500;
20+
}
21+
22+
const queryClient: QueryClient = new QueryClient({
23+
defaultOptions: {
24+
queries: {
25+
retry: (failureCount: number, error: unknown): boolean => {
26+
if (!isRetryableError(error)) return false;
27+
return failureCount < 3;
28+
},
29+
retryDelay: (attempt: number) => Math.min(1000 * 2 ** attempt, 30000),
30+
},
31+
mutations: {
32+
retry: (failureCount: number, error: unknown): boolean => {
33+
if (!isRetryableError(error)) return false;
34+
return failureCount < 1;
35+
},
36+
retryDelay: (attempt: number) => Math.min(1000 * 2 ** attempt, 30000),
37+
},
38+
},
39+
});
1440

1541
/**
1642
* Renders a CalProvider component.
@@ -44,7 +70,7 @@ export function CalProvider({
4470
version = VERSION_2024_06_14,
4571
organizationId,
4672
isEmbed = false,
47-
}: CalProviderProps) {
73+
}: CalProviderProps): JSX.Element {
4874
useEffect(() => {
4975
http.setVersionHeader(version);
5076
}, [version]);
@@ -74,7 +100,8 @@ export function CalProvider({
74100
version={version}
75101
labels={labels as Record<translationKeys, string>}
76102
language={language}
77-
organizationId={organizationId}>
103+
organizationId={organizationId}
104+
>
78105
{children}
79106
</BaseCalProvider>
80107
</QueryClientProvider>

0 commit comments

Comments
 (0)