fix(core): exponential retry on cache put fail (#29017)

Co-authored-by: Mike <mike.pham@autogeneral.com.au>
This commit is contained in:
Mike Pham 2025-02-01 01:02:45 +10:00 committed by GitHub
parent 82bfb99366
commit 0d9d3fef9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -555,17 +555,20 @@ function tryAndRetry<T>(fn: () => Promise<T>): Promise<T> {
let attempts = 0; let attempts = 0;
// Generate a random number between 2 and 4 to raise to the power of attempts // Generate a random number between 2 and 4 to raise to the power of attempts
const baseExponent = Math.random() * 2 + 2; const baseExponent = Math.random() * 2 + 2;
const baseTimeout = 15;
const _try = async () => { const _try = async () => {
try { try {
attempts++; attempts++;
return await fn(); return await fn();
} catch (e) { } catch (e) {
// Max time is 5 * 4^3 = 20480ms // Max time is 15 * (4 + 4² + 4³ + 4⁴ + 4⁵) = 20460ms
if (attempts === 6) { if (attempts === 6) {
// After enough attempts, throw the error // After enough attempts, throw the error
throw e; throw e;
} }
await new Promise((res) => setTimeout(res, baseExponent ** attempts)); await new Promise((res) =>
setTimeout(res, baseTimeout * baseExponent ** attempts)
);
return await _try(); return await _try();
} }
}; };