Skip to content

Commit

Permalink
fix: add min delay to exp backoff (#12488)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhockett authored May 1, 2023
1 parent 91826bb commit 6885e01
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export const executeSdkPromisesWithExponentialBackOff = async <T>(sdkPromises: (() => Promise<T>)[]): Promise<T[]> => {
const MAX_RETRIES = 5;
const MAX_BACK_OFF_IN_MS = 10 * 1000; // 10 seconds
let backOffSleepTimeInMs = 200;
const MIN_BACK_OFF_IN_MS = 1000; // 1 second
let backOffSleepTimeInMs = 500;
let consecutiveRetries = 0;

let i = 0;
Expand All @@ -11,15 +12,15 @@ export const executeSdkPromisesWithExponentialBackOff = async <T>(sdkPromises: (
promiseResults.push(await sdkPromises[i]());
++i;
// In case previously throttled, reset backoff
backOffSleepTimeInMs = 200;
backOffSleepTimeInMs = 500;
consecutiveRetries = 0;
} catch (e) {
if (e?.code === 'ThrottlingException' || e?.code === 'Throttling') {
if (consecutiveRetries < MAX_RETRIES) {
++consecutiveRetries;
await new Promise((resolve) => setTimeout(resolve, backOffSleepTimeInMs));
backOffSleepTimeInMs = 2 ** consecutiveRetries * backOffSleepTimeInMs;
backOffSleepTimeInMs = Math.min(Math.random() * backOffSleepTimeInMs, MAX_BACK_OFF_IN_MS);
backOffSleepTimeInMs = Math.max(Math.min(Math.random() * backOffSleepTimeInMs, MAX_BACK_OFF_IN_MS), MIN_BACK_OFF_IN_MS);
continue;
}
}
Expand Down

0 comments on commit 6885e01

Please sign in to comment.