chore: move retryOnThrottled to frontend-shared
This commit is contained in:
parent
e36ea27517
commit
de7f7984cd
2 changed files with 42 additions and 42 deletions
40
packages/frontend-shared/js/retry-on-throttled.ts
Normal file
40
packages/frontend-shared/js/retry-on-throttled.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
async function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
window.setTimeout(() => {
|
||||
resolve();
|
||||
}, ms);
|
||||
});
|
||||
}
|
||||
|
||||
export async function retryOnThrottled<T>(f: ()=>Promise<T>, retryCount = 5): Promise<T> {
|
||||
let lastOk = false;
|
||||
let lastResultOrError: T;
|
||||
for (let i = 0; i < retryCount; i++) {
|
||||
const [ok, resultOrError] = await f()
|
||||
.then(result => [true, result])
|
||||
.catch(err => [false, err]);
|
||||
|
||||
lastOk = ok;
|
||||
lastResultOrError = resultOrError;
|
||||
|
||||
if (ok) {
|
||||
break;
|
||||
}
|
||||
|
||||
// RATE_LIMIT_EXCEEDED
|
||||
if (resultOrError?.id === 'd5826d14-3982-4d2e-8011-b9e9f02499ef') {
|
||||
await sleep(resultOrError?.info?.fullResetMs ?? 1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Throw for non-throttling errors
|
||||
throw resultOrError;
|
||||
}
|
||||
|
||||
if (lastOk) {
|
||||
return lastResultOrError!;
|
||||
} else {
|
||||
// Give up after getting throttled too many times
|
||||
throw lastResultOrError!;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue