diff --git a/.changeset/silly-bottles-raise.md b/.changeset/silly-bottles-raise.md new file mode 100644 index 0000000000..880d976680 --- /dev/null +++ b/.changeset/silly-bottles-raise.md @@ -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. diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index 404d201a44..f0b6793c3f 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -181,6 +181,7 @@ export async function createRouter( let cursor: Cursor | undefined; try { + let currentWrite: Promise | 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();