From ad3eddbc08ce44191aad3003f083851869c201d1 Mon Sep 17 00:00:00 2001 From: Owen Date: Sun, 21 Jun 2026 17:34:46 -0400 Subject: [PATCH] Batch get olm ids --- server/lib/rebuildClientAssociations.ts | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/server/lib/rebuildClientAssociations.ts b/server/lib/rebuildClientAssociations.ts index f6a94e7b7..8e235bc48 100644 --- a/server/lib/rebuildClientAssociations.ts +++ b/server/lib/rebuildClientAssociations.ts @@ -629,6 +629,21 @@ async function handleMessagesForSiteClients( } } + // Batch-fetch all olm IDs for the clients we need to process + const clientIdsToProcess = Array.from(clientsToProcess.keys()); + const olmRows = + clientIdsToProcess.length > 0 + ? await trx + .select({ olmId: olms.olmId, clientId: olms.clientId }) + .from(olms) + .where(inArray(olms.clientId, clientIdsToProcess)) + : []; + const olmByClientId = new Map( + olmRows + .filter((r) => r.clientId !== null) + .map((r) => [r.clientId as number, r.olmId]) + ); + for (const client of clientsToProcess.values()) { // UPDATE THE NEWT if (!client.subnet || !client.pubKey) { @@ -645,14 +660,8 @@ async function handleMessagesForSiteClients( continue; } - const [olm] = await trx - .select({ - olmId: olms.olmId - }) - .from(olms) - .where(eq(olms.clientId, client.clientId)) - .limit(1); - if (!olm) { + const olmId = olmByClientId.get(client.clientId); + if (!olmId) { logger.warn( `Olm not found for client ${client.clientId} so cannot add/delete peers` ); @@ -669,7 +678,7 @@ async function handleMessagesForSiteClients( clientId: client.clientId, siteId, publicKey: site.publicKey, - olmId: olm.olmId + olmId }); } @@ -691,7 +700,7 @@ async function handleMessagesForSiteClients( endpoint: exitNode.endpoint } }, - olmId: olm.olmId + olmId }); }