@@ -12,6 +12,7 @@ import { logger } from "@src/utils/logger";
1212import { getVaccineTypeFromLowercaseString } from "@src/utils/path" ;
1313import { RequestContext , asyncLocalStorage } from "@src/utils/requestContext" ;
1414import { Context } from "aws-lambda" ;
15+ import { retry } from "es-toolkit" ;
1516
1617const 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