Skip to content

Commit 631de1b

Browse files
committed
Feat : added retry logic for object storage fetch failures
1 parent 9fdf91a commit 631de1b

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

apps/webapp/app/v3/r2.server.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,34 @@ export async function downloadPacketFromObjectStore(
9393

9494
logger.debug("Downloading from object store", { url: url.href });
9595

96-
const response = await r2.fetch(url.toString());
96+
async function fetchWithRetry(url:string,retries =3,delay=500):Promise<Response>{
97+
for(let attempt =1; attempt<=retries;attempt++){
98+
try {
99+
const response = await r2.fetch(url);
100+
if(response.ok) return response;
101+
102+
// only retry on transient server/network errors
103+
if(response.status >= 500 && response.status<600 ){
104+
throw new Error(`Server Error : ${response.statusText}`);
105+
}
106+
107+
// for other non server errors
108+
throw new Error(`non-retryable error :${response.statusText}`);
109+
110+
} catch (error:any) {
111+
if(attempt == retries) throw error;
112+
logger.warn(`Retrying object Download (attempt: ${attempt}) out of ${retries}`,{
113+
url,
114+
error:error.message,
115+
});
116+
await new Promise((res)=>setTimeout(res,delay*attempt));
117+
}
118+
}
119+
120+
throw new Error(`Failed to fetch ${url} after ${retries} retries`);
97121

98-
if (!response.ok) {
99-
throw new Error(`Failed to download input from ${url}: ${response.statusText}`);
100122
}
123+
const response = await fetchWithRetry(url.toString());
101124

102125
const data = await response.text();
103126

0 commit comments

Comments
 (0)