Skip to content

Commit

Permalink
fix: hotswap for functions uses exponential backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowCat567 committed Nov 18, 2024
1 parent 6820c62 commit 044c874
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 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,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,
Expand Down Expand Up @@ -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<any>, numOfRetries: number, errorCodeToRetry: string) {
async function exponentialBackoffRetry(fn: () => Promise<any>, 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;
}
Expand Down

0 comments on commit 044c874

Please sign in to comment.