From 044c8746b9d5751667ad5b3596d13a8a4ac56e1b Mon Sep 17 00:00:00 2001 From: Leena V Date: Mon, 18 Nov 2024 23:35:58 +0000 Subject: [PATCH] fix: hotswap for functions uses exponential backoff --- .../lib/api/hotswap/appsync-mapping-templates.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts b/packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts index d05aa56063339..aec6b81e388e2 100644 --- a/packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts +++ b/packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts @@ -118,7 +118,7 @@ export async function isHotswappableAppSyncChange( const functions = await sdk.appsync().listFunctions({ apiId: sdkRequestObject.apiId }); const { functionId } = functions.find((fn) => fn.name === physicalName) ?? {}; // Updating multiple functions at the same time or along with graphql schema results in `ConcurrentModificationException` - await simpleRetry( + await exponentialBackoffRetry( () => sdk.appsync().updateFunction({ ...sdkRequestObject, @@ -169,13 +169,13 @@ async function fetchFileFromS3(s3Url: string, sdk: SDK) { return (await sdk.s3().getObject({ Bucket: s3Bucket, Key: s3Key })).Body?.transformToString(); } -async function simpleRetry(fn: () => Promise, numOfRetries: number, errorCodeToRetry: string) { +async function exponentialBackoffRetry(fn: () => Promise, backoff: number, errorCodeToRetry: string) { try { await fn(); } catch (error: any) { - if (error && error.name === errorCodeToRetry && numOfRetries > 0) { - await sleep(1000); // wait a whole second - await simpleRetry(fn, numOfRetries - 1, errorCodeToRetry); + if (error && error.name === errorCodeToRetry) { + await sleep(backoff); // time to wait doubles everytime function fails, starts at 1 second + await exponentialBackoffRetry(fn, backoff * 2, errorCodeToRetry); } else { throw error; }