Skip to content

Commit ed07f3f

Browse files
Merge pull request #96 from useblacksmith/add-fetch-retry
Add retry logic with exponential backoff to fetch calls
2 parents c27f005 + 4f4a036 commit ed07f3f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

dist/index.js

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import { getInput, setFailed, info } from "@actions/core";
2-
import fetch from "node-fetch";
2+
import fetch, { RequestInit, Response } from "node-fetch";
3+
4+
async function fetchWithRetry(url: string, options: RequestInit, maxRetries = 3): Promise<Response> {
5+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
6+
try {
7+
const response = await fetch(url, options);
8+
return response;
9+
} catch (error) {
10+
if (attempt === maxRetries) {
11+
throw error;
12+
}
13+
14+
const delay = Math.pow(2, attempt) * 1000;
15+
await new Promise(resolve => setTimeout(resolve, delay));
16+
}
17+
}
18+
19+
throw new Error("Retry loop completed without return or throw");
20+
}
321

422
interface DeleteCacheParams {
523
cacheKey: string;
@@ -36,7 +54,7 @@ export async function deleteCache({
3654
const resource = cacheVersion ? `${cacheKey}/${cacheVersion}` : cacheKey;
3755
const url = `${baseUrl}/caches/${resource}`;
3856

39-
const response = await fetch(prefix ? `${url}?prefix` : url, {
57+
const response = await fetchWithRetry(prefix ? `${url}?prefix` : url, {
4058
method: "DELETE",
4159
headers: {
4260
Accept: "application/json; version=6.0-preview.1",

0 commit comments

Comments
 (0)