Initial commit
This commit is contained in:
30
app/utils/download.ts
Normal file
30
app/utils/download.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export const TRANSIENT_DOWNLOAD_STATUSES = [
|
||||
408, 425, 429, 500, 502, 503, 504,
|
||||
] as const;
|
||||
export function leaseStorageKey(id: string): string {
|
||||
return `quickdrop-lease:${id}`;
|
||||
}
|
||||
export async function retryTransient(
|
||||
operation: () => Promise<Response>,
|
||||
attempts = 5,
|
||||
): Promise<Response> {
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
const response = await operation();
|
||||
if (
|
||||
response.ok ||
|
||||
!TRANSIENT_DOWNLOAD_STATUSES.includes(response.status as never) ||
|
||||
attempt >= attempts - 1
|
||||
)
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (attempt >= attempts - 1) throw error;
|
||||
}
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(
|
||||
resolve,
|
||||
Math.round(500 * 2 ** attempt * (0.8 + Math.random() * 0.4)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user