Skip to content

Commit 0a8cb8c

Browse files
VIA-615 AJ Add logging and increase delay to avoid rate limits
1 parent ea82c76 commit 0a8cb8c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,24 @@ const runContentCacheHydrator = async (event: ContentCacheHydratorEvent) => {
149149
let invalidatedCount: number = 0;
150150

151151
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`);
152+
const rateLimitDelayWithMargin: number = 2 * rateLimitDelayMillis; // to keep ourselves well within the budget
153+
log.info(`Delay used between calls to rate limit content API is ${rateLimitDelayWithMargin}ms`);
154154
for (const vaccine of vaccinesToRunOn) {
155155
const status = await retry(
156156
async () => hydrateCacheForVaccine(vaccine, await config.CONTENT_CACHE_IS_CHANGE_APPROVAL_ENABLED, forceUpdate),
157157
{
158158
retries: 3,
159-
delay: (attempt) => rateLimitDelayMillis * Math.pow(2, attempt - 1),
159+
delay: (attempt) => {
160+
const delayMillis = rateLimitDelayWithMargin * Math.pow(2, attempt);
161+
log.warn({ context: { vaccine, attempt, delayMillis } }, "Failed to hydrate cache, trying again");
162+
return delayMillis;
163+
},
160164
},
161165
);
162166

163167
invalidatedCount += status.invalidatedCount;
164168
failureCount += status.failureCount;
165-
await new Promise((f) => setTimeout(f, rateLimitDelayMillis * rateLimitDelayMarginFactor)); // sleep
169+
await new Promise((f) => setTimeout(f, rateLimitDelayWithMargin)); // sleep
166170
}
167171

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

0 commit comments

Comments
 (0)