Skip to content

Commit

Permalink
Merge pull request #11 from GooeyAI/fix-error-retries
Browse files Browse the repository at this point in the history
fix: retry only on specific 5xx errors
  • Loading branch information
nikochiko authored Sep 4, 2024
2 parents 38bc185 + 220d1af commit e82ecd7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
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);
});
});

0 comments on commit e82ecd7

Please sign in to comment.