mirror of
https://github.com/certd/certd.git
synced 2026-06-28 14:37:32 +08:00
This reverts commit 56b8c689ec.
This commit is contained in:
@@ -188,51 +188,4 @@ export class AwsClient {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retries an AWS SDK call with exponential backoff when throttled.
|
||||
* Handles: TooManyRequestsException, ThrottlingException, RequestLimitExceeded, Throttling
|
||||
*/
|
||||
async withRetry<T>(call: () => Promise<T>, maxAttempts = 5, baseDelayMs = 2000): Promise<T> {
|
||||
const throttlingCodes = new Set([
|
||||
"TooManyRequestsException",
|
||||
"ThrottlingException",
|
||||
"RequestLimitExceeded",
|
||||
"Throttling",
|
||||
]);
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await call();
|
||||
} catch (err: any) {
|
||||
const code = err?.name || err?.Code || err?.code || "";
|
||||
const isThrottle = throttlingCodes.has(code) || err?.message?.toLowerCase().includes("rate exceeded");
|
||||
if (isThrottle && attempt < maxAttempts) {
|
||||
const delay = baseDelayMs * Math.pow(2, attempt - 1); // 2s, 4s, 8s, 16s …
|
||||
this.logger.warn(`AWS rate limit hit (${code}), attempt ${attempt}/${maxAttempts}, retrying in ${delay}ms…`);
|
||||
await utils.sleep(delay);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls a CloudFront distribution until its Status becomes "Deployed".
|
||||
* CloudFront propagates changes globally and can take several minutes.
|
||||
*/
|
||||
async waitForDistributionDeployed(cloudFrontClient: any, distributionId: string, timeoutMs = 600_000, pollIntervalMs = 15_000): Promise<void> {
|
||||
const { GetDistributionCommand } = await import("@aws-sdk/client-cloudfront");
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
const res = await this.withRetry(() => cloudFrontClient.send(new GetDistributionCommand({ Id: distributionId })));
|
||||
const status = res?.Distribution?.Status;
|
||||
this.logger.info(`CloudFront distribution ${distributionId} status: ${status}`);
|
||||
if (status === "Deployed") {
|
||||
return;
|
||||
}
|
||||
await utils.sleep(pollIntervalMs);
|
||||
}
|
||||
throw new Error(`Timed out waiting for CloudFront distribution ${distributionId} to reach Deployed status`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user