Skip to content
Open
Changes from 2 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
34 changes: 31 additions & 3 deletions apps/webapp/app/v3/r2.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,39 @@ export async function downloadPacketFromObjectStore(

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

const response = await r2.fetch(url.toString());
async function fetchWithRetry(url:string,retries =3,delay=500):Promise<Response>{

if (!r2) {
throw new Error("Object store credentials are not set");
}

for(let attempt =1; attempt<=retries;attempt++){
try {
const response = await r2.fetch(url);
if(response.ok) return response;

// only retry on transient server/network errors
if(response.status >= 500 && response.status<600 ){
throw new Error(`Server Error : ${response.statusText}`);
}

// for other non server errors
throw new Error(`non-retryable error :${response.statusText}`);

} catch (error:any) {
if(attempt == retries) throw error;
logger.warn(`Retrying object Download (attempt: ${attempt}) out of ${retries}`,{
url,
error:error.message,
});
await new Promise((res)=>setTimeout(res,delay*attempt));
}
}

throw new Error(`Failed to fetch ${url} after ${retries} retries`);

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

const data = await response.text();

Expand Down