Skip to content

Commit 933f83e

Browse files
committed
chore: add retry and exponential backoff to avoid too many requests exception
Signed-off-by: Kevin Shan <[email protected]>
1 parent 25e4cae commit 933f83e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

packages/amplify-codegen-e2e-tests/src/cleanup-e2e-resources.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,25 @@ const handleExpiredTokenException = (): void => {
117117
* @param region aws region to check
118118
* @returns true if region is enabled in that account, false otherwise
119119
*/
120-
const isRegionEnabled = async (accountInfo: AWSAccountInfo, region: string): Promise<boolean> => {
121-
const account = new Account({ ...accountInfo, region: 'us-east-1' });
122-
const optStatus = await account.getRegionOptStatus({ RegionName: region }).promise();
123-
124-
return optStatus.RegionOptStatus === 'ENABLED' || optStatus.RegionOptStatus === 'ENABLED_BY_DEFAULT';
120+
const isRegionEnabled = async (accountInfo: AWSAccountInfo, region: string, maxRetries = 3): Promise<boolean> => {
121+
for (let attempt = 0; attempt < maxRetries; attempt++) {
122+
try {
123+
const account = new Account({ ...accountInfo, region: 'us-east-1' });
124+
const optStatus = await account.getRegionOptStatus({ RegionName: region }).promise();
125+
126+
return optStatus.RegionOptStatus === 'ENABLED' || optStatus.RegionOptStatus === 'ENABLED_BY_DEFAULT';
127+
} catch (error) {
128+
if (error.code === 'TooManyRequestsException') {
129+
const backoffTime = Math.pow(2, attempt) * 1000; // exponential backoff: 1s, 2s, 4s
130+
if (attempt < maxRetries - 1) {
131+
await sleep(backoffTime);
132+
continue;
133+
}
134+
}
135+
throw error; // rethrow if it's not a TooManyRequestsException or we're out of retries
136+
}
137+
}
138+
throw new Error('Max retries exceeded while checking region status');
125139
};
126140

127141
/**

0 commit comments

Comments
 (0)