Skip to content

Commit e52ae7f

Browse files
committed
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.
1 parent 1a933c9 commit e52ae7f

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

packages/angular/ssr/src/app-engine.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class AngularAppEngine {
5050
/**
5151
* A cache that holds entry points, keyed by their potential locale string.
5252
*/
53-
private readonly entryPointsCache = new Map<string, EntryPointExports>();
53+
private readonly entryPointsCache = new Map<string, Promise<EntryPointExports>>();
5454

5555
/**
5656
* Renders a response for the given HTTP request using the server application.
@@ -110,9 +110,7 @@ export class AngularAppEngine {
110110
* @param potentialLocale - The locale string used to find the corresponding entry point.
111111
* @returns A promise that resolves to the entry point exports or `undefined` if not found.
112112
*/
113-
private async getEntryPointExports(
114-
potentialLocale: string,
115-
): Promise<EntryPointExports | undefined> {
113+
private getEntryPointExports(potentialLocale: string): Promise<EntryPointExports> | undefined {
116114
const cachedEntryPoint = this.entryPointsCache.get(potentialLocale);
117115
if (cachedEntryPoint) {
118116
return cachedEntryPoint;
@@ -124,7 +122,7 @@ export class AngularAppEngine {
124122
return undefined;
125123
}
126124

127-
const entryPointExports = await entryPoint();
125+
const entryPointExports = entryPoint();
128126
this.entryPointsCache.set(potentialLocale, entryPointExports);
129127

130128
return entryPointExports;
@@ -141,7 +139,7 @@ export class AngularAppEngine {
141139
* @param url - The URL of the request.
142140
* @returns A promise that resolves to the entry point exports or `undefined` if not found.
143141
*/
144-
private getEntryPointExportsForUrl(url: URL): Promise<EntryPointExports | undefined> {
142+
private getEntryPointExportsForUrl(url: URL): Promise<EntryPointExports> | undefined {
145143
const { entryPoints, basePath } = this.manifest;
146144
if (entryPoints.size === 1) {
147145
return this.getEntryPointExports('');

0 commit comments

Comments
 (0)