Skip to content

Commit b011e27

Browse files
add retryOptions
1 parent 2683e51 commit b011e27

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

sdk/src/network-client.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,19 @@ class AleoNetworkClient {
100100
* @param url The URL to fetch data from.
101101
*/
102102
async fetchData<Type>(url = "/"): Promise<Type> {
103-
try {
104-
return retryWithBackoff(() => this.fetchRaw(url).then(parseJSON));
105-
} catch (error) {
106-
throw new Error(`Error fetching data: ${error}`);
107-
}
108-
}
103+
try {
104+
return await retryWithBackoff(() => this.fetchRaw(url).then(parseJSON), {
105+
retryOnStatus: [500, 502, 503, 504],
106+
shouldRetry: (err) => {
107+
const msg = err?.message?.toLowerCase?.() || "";
108+
return msg.includes("network") || msg.includes("timeout") || msg.includes("503");
109+
}
110+
});
111+
} catch (error) {
112+
throw new Error(`Error fetching data: ${error}`);
113+
}
114+
}
115+
109116

110117
/**
111118
* Fetches data from the Aleo network and returns it as an unparsed string.

sdk/src/utils.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,43 @@ export async function post(url: URL | string, options: RequestInit) {
4040
return response;
4141
}
4242

43-
export async function retryWithBackoff<T>(
43+
type RetryOptions = {
44+
maxAttempts?: number;
45+
baseDelay?: number;
46+
retryOnStatus?: number[]; // e.g. [500, 502, 503]
47+
shouldRetry?: (err: any) => boolean;
48+
};
49+
50+
export async function retryWithBackoff<T>(
4451
fn: () => Promise<T>,
45-
maxAttempts = 5,
46-
baseDelay = 100
52+
{
53+
maxAttempts = 5,
54+
baseDelay = 100,
55+
retryOnStatus = [],
56+
shouldRetry,
57+
}: RetryOptions = {}
4758
): Promise<T> {
4859
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
4960
try {
5061
return await fn();
5162
} catch (err: any) {
52-
const error = err as Error & { code?: string };
5363
const isLast = attempt === maxAttempts;
5464

55-
// retry only for certain errors
65+
const error = err as Error & { code?: string; status?: number };
66+
5667
const retryable =
57-
// err?. 404s but that will probably not be 404 soon ?
58-
error?.message?.includes('404') ||
59-
// retry for timeouts as well
60-
error?.message?.includes('5');
68+
(typeof error.status === "number" && retryOnStatus.includes(error.status)) ||
69+
error.message?.includes("5") ||
70+
error.message?.includes("404") ||
71+
shouldRetry?.(error);
6172

6273
if (!retryable || isLast) throw error;
6374

6475
const jitter = Math.floor(Math.random() * baseDelay);
6576
const delay = baseDelay * 2 ** (attempt - 1) + jitter;
6677
console.warn(`Retry ${attempt}/${maxAttempts} failed. Retrying in ${delay}ms...`);
6778

68-
await new Promise(res => setTimeout(res, delay));
79+
await new Promise((res) => setTimeout(res, delay));
6980
}
7081
}
7182

0 commit comments

Comments
 (0)