Skip to content

Commit

Permalink
Updating backoff policy to use retry in backoffOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Feb 23, 2024
1 parent 0b2a3ff commit 046b4f5
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,36 +460,24 @@ export const generateLzConfig = async (
* @returns {Promise<T>} - A promise resolving to the result of the operation if successful.
* @throws {Error} - Throws an error if all attempts fail.
*/
async function basicRetryPolicy<T>(
fn: () => Promise<T>,
maxAttempts: number = 3,
baseDelay: number = 1000,
maxDelay: number = 15000
): Promise<T> {
async function basicRetryPolicy<T>(fn: () => Promise<T>, maxAttempts: number = 3): Promise<T> {
const operation = async () => {
return await fn()
}

const backoffOptions = {
numOfAttempts: maxAttempts,
startingDelay: baseDelay,
delayFirstAttempt: true,
maxDelay: maxDelay,
factor: 2,
randomisationFactor: 0.5,
}

for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
logger.verbose(`Attempt ${attempt + 1}/${maxAttempts}`)
return await backOff(operation, backoffOptions)
} catch (error) {
logger.info(`Attempt ${attempt + 1} failed with error: ${error}`)
if (attempt < maxAttempts - 1) {
retry: (e: any, attemptNumber: number) => {

Check warning on line 470 in packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts

View workflow job for this annotation

GitHub Actions / Check code / Build & Lint

Unexpected any. Specify a different type
logger.info(`Attempt ${attemptNumber}/${maxAttempts} failed with error: ${e}`)
if (attemptNumber < maxAttempts) {
logger.info('Retrying...')
return true
} else {
logger.info(`All ${maxAttempts} attempts failed.`)
return false
}
}
},
}

throw new Error(`All ${maxAttempts} attempts failed`)
return await backOff(operation, backoffOptions)
}

0 comments on commit 046b4f5

Please sign in to comment.