catalog-backend: better concurrency and more eager response writing

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-17 12:43:36 +01:00
parent 8a42837cf3
commit be0aae7467
2 changed files with 15 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Improved concurrency of the `entities` endpoint when using the streamed query mode behind the `catalog.disableRelationsCompatibility` flag.
@@ -181,6 +181,7 @@ export async function createRouter(
let cursor: Cursor | undefined;
try {
let currentWrite: Promise<boolean> | undefined = undefined;
do {
const result = await entitiesCatalog.queryEntities(
!cursor
@@ -195,15 +196,21 @@ export async function createRouter(
: { credentials, fields, limit, cursor },
);
// Wait for previous write to complete
if (await currentWrite) {
return; // Client closed connection
}
if (result.items.entities.length) {
if (await responseStream.send(result.items)) {
return; // Client closed connection
}
currentWrite = responseStream.send(result.items);
}
cursor = result.pageInfo?.nextCursor;
} while (cursor);
// Wait for last write to complete
await currentWrite;
responseStream.complete();
} finally {
responseStream.close();