Skip to content

Commit

Permalink
perf(@angular/ssr): prevent potential stampede in entry-points cache
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkrems committed Sep 26, 2024
1 parent 1a933c9 commit e52ae7f
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/angular/ssr/src/app-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class AngularAppEngine {
/**
* A cache that holds entry points, keyed by their potential locale string.
*/
private readonly entryPointsCache = new Map<string, EntryPointExports>();
private readonly entryPointsCache = new Map<string, Promise<EntryPointExports>>();

/**
* Renders a response for the given HTTP request using the server application.
Expand Down Expand Up @@ -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<EntryPointExports | undefined> {
private getEntryPointExports(potentialLocale: string): Promise<EntryPointExports> | undefined {
const cachedEntryPoint = this.entryPointsCache.get(potentialLocale);
if (cachedEntryPoint) {
return cachedEntryPoint;
Expand All @@ -124,7 +122,7 @@ export class AngularAppEngine {
return undefined;
}

const entryPointExports = await entryPoint();
const entryPointExports = entryPoint();
this.entryPointsCache.set(potentialLocale, entryPointExports);

return entryPointExports;
Expand All @@ -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<EntryPointExports | undefined> {
private getEntryPointExportsForUrl(url: URL): Promise<EntryPointExports> | undefined {
const { entryPoints, basePath } = this.manifest;
if (entryPoints.size === 1) {
return this.getEntryPointExports('');
Expand Down

0 comments on commit e52ae7f

Please sign in to comment.