Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry only on specific 5xx errors #11

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
src/GooeyClient.ts
src/index.ts
src/core/fetcher/requestWithRetries.ts
tests/unit/fetcher/requestWithRetries.test.ts
2 changes: 1 addition & 1 deletion src/core/fetcher/requestWithRetries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function requestWithRetries(
let response: Response = await requestFn();

for (let i = 0; i < maxRetries; ++i) {
if ([408, 409, 429].includes(response.status) || response.status >= 500) {
if ([408, 409, 429, 502, 503, 504].includes(response.status)) {
const delay = Math.min(INITIAL_RETRY_DELAY * Math.pow(2, i), MAX_RETRY_DELAY);
await new Promise((resolve) => setTimeout(resolve, delay));
response = await requestFn();
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/fetcher/requestWithRetries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Test exponential backoff", () => {
.mockResolvedValueOnce(new Response("", { status: 408 }))
.mockResolvedValueOnce(new Response("", { status: 409 }))
.mockResolvedValueOnce(new Response("", { status: 429 }))
.mockResolvedValueOnce(new Response("", { status: 500 }))
.mockResolvedValueOnce(new Response("", { status: 503 }))
.mockResolvedValueOnce(new Response("", { status: 502 }))
.mockResolvedValueOnce(new Response("", { status: 200 }))
.mockResolvedValueOnce(new Response("", { status: 408 }));
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("Test exponential backoff", () => {
});

it("should retry with exponential backoff timing", async () => {
mockFetch.mockResolvedValue(new Response("", { status: 500 }));
mockFetch.mockResolvedValue(new Response("", { status: 502 }));
const maxRetries = 7;
const responsePromise = requestWithRetries(() => mockFetch(), maxRetries);
expect(mockFetch).toHaveBeenCalledTimes(1);
Expand All @@ -80,6 +80,6 @@ describe("Test exponential backoff", () => {
expect(mockFetch).toHaveBeenCalledTimes(Math.min(i + 2, maxRetries + 1));
}
const response = await responsePromise;
expect(response.status).toBe(500);
expect(response.status).toBe(502);
});
});
Loading