From 436bcf63de8d9658c5e9483438af02354edb39c7 Mon Sep 17 00:00:00 2001 From: Jan Martin Date: Thu, 26 Sep 2024 08:17:00 -0700 Subject: [PATCH] perf(@angular/ssr): prevent potential stampede in entry-points cache If multiple concurrent requests hit `getEntryPointExports`, all of them would previously see the cache miss for entry point. With this change, only the first request will and the others can leverage the cache. This can be important when instances are added to a pool under high traffic. --- packages/angular/ssr/src/app-engine.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/angular/ssr/src/app-engine.ts b/packages/angular/ssr/src/app-engine.ts index 9862901c7c4f..31b2e804f23c 100644 --- a/packages/angular/ssr/src/app-engine.ts +++ b/packages/angular/ssr/src/app-engine.ts @@ -50,7 +50,7 @@ export class AngularAppEngine { /** * A cache that holds entry points, keyed by their potential locale string. */ - private readonly entryPointsCache = new Map(); + private readonly entryPointsCache = new Map>(); /** * Renders a response for the given HTTP request using the server application. @@ -110,9 +110,7 @@ export class AngularAppEngine { * @param potentialLocale - The locale string used to find the corresponding entry point. * @returns A promise that resolves to the entry point exports or `undefined` if not found. */ - private async getEntryPointExports( - potentialLocale: string, - ): Promise { + private getEntryPointExports(potentialLocale: string): Promise | undefined { const cachedEntryPoint = this.entryPointsCache.get(potentialLocale); if (cachedEntryPoint) { return cachedEntryPoint; @@ -124,7 +122,7 @@ export class AngularAppEngine { return undefined; } - const entryPointExports = await entryPoint(); + const entryPointExports = entryPoint(); this.entryPointsCache.set(potentialLocale, entryPointExports); return entryPointExports; @@ -141,7 +139,7 @@ export class AngularAppEngine { * @param url - The URL of the request. * @returns A promise that resolves to the entry point exports or `undefined` if not found. */ - private getEntryPointExportsForUrl(url: URL): Promise { + private getEntryPointExportsForUrl(url: URL): Promise | undefined { const { entryPoints, basePath } = this.manifest; if (entryPoints.size === 1) { return this.getEntryPointExports('');