Skip to content

Commit

Permalink
fix: added retry limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowCat567 committed Nov 19, 2024
1 parent 2d869f2 commit 633c9ea
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ 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 exponentialBackoffRetry(
await exponentialBackOffRetry(
() =>
sdk.appsync().updateFunction({
...sdkRequestObject,
functionId: functionId,
}),
6,
1000,
'ConcurrentModificationException',
);
Expand Down Expand Up @@ -169,13 +170,13 @@ async function fetchFileFromS3(s3Url: string, sdk: SDK) {
return (await sdk.s3().getObject({ Bucket: s3Bucket, Key: s3Key })).Body?.transformToString();
}

async function exponentialBackoffRetry(fn: () => Promise<any>, backoff: number, errorCodeToRetry: string) {
async function exponentialBackOffRetry(fn: () => Promise<any>, numOfRetries: number, backOff: number, errorCodeToRetry: string) {
try {
await fn();
} catch (error: any) {
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);
if (error && error.name === errorCodeToRetry && numOfRetries > 0) {
await sleep(backOff); // time to wait doubles everytime function fails, starts at 1 second
await exponentialBackOffRetry(fn, numOfRetries - 1, backOff * 2, errorCodeToRetry);
} else {
throw error;
}
Expand Down

0 comments on commit 633c9ea

Please sign in to comment.