Skip to content

Commit 648e588

Browse files
VIA-615 AJ/AS Add a rate limit delay margin factor to stay within rate limits
- cache hydrator still gets rate limited sometimes
1 parent 4b33821 commit 648e588

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/_lambda/content-cache-hydrator/handler.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { logger } from "@src/utils/logger";
1212
import { getVaccineTypeFromLowercaseString } from "@src/utils/path";
1313
import { RequestContext, asyncLocalStorage } from "@src/utils/requestContext";
1414
import { Context } from "aws-lambda";
15+
import { retry } from "es-toolkit";
1516

1617
const log = logger.child({ module: "content-cache-hydrator" });
1718

@@ -148,15 +149,20 @@ const runContentCacheHydrator = async (event: ContentCacheHydratorEvent) => {
148149
let invalidatedCount: number = 0;
149150

150151
const rateLimitDelayMillis: number = 1000 / ((await config.CONTENT_API_RATE_LIMIT_PER_MINUTE) / 60);
152+
const rateLimitDelayMarginFactor: number = 2; // to keep ourselves well within the budget
153+
log.info(`Rate limit delay for content API is ${rateLimitDelayMillis}ms`);
151154
for (const vaccine of vaccinesToRunOn) {
152-
const status = await hydrateCacheForVaccine(
153-
vaccine,
154-
await config.CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED,
155-
forceUpdate,
155+
const status = await retry(
156+
async () => hydrateCacheForVaccine(vaccine, await config.CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED, forceUpdate),
157+
{
158+
retries: 3,
159+
delay: (attempt) => rateLimitDelayMillis * Math.pow(2, attempt - 1),
160+
},
156161
);
162+
157163
invalidatedCount += status.invalidatedCount;
158164
failureCount += status.failureCount;
159-
await new Promise((f) => setTimeout(f, rateLimitDelayMillis)); // sleep
165+
await new Promise((f) => setTimeout(f, rateLimitDelayMillis * rateLimitDelayMarginFactor)); // sleep
160166
}
161167

162168
log.info({ context: { failureCount, invalidatedCount } }, "Finished hydrating content cache: report");

0 commit comments

Comments
 (0)