From 58185d29ca95daa568085b11f1afda4aeb3ad897 Mon Sep 17 00:00:00 2001 From: silberengel Date: Sun, 24 Aug 2025 09:34:06 +0200 Subject: [PATCH] limit search size --- src/lib/utils/search_constants.ts | 3 +++ src/lib/utils/subscription_search.ts | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lib/utils/search_constants.ts b/src/lib/utils/search_constants.ts index aa8fe6c..1326898 100644 --- a/src/lib/utils/search_constants.ts +++ b/src/lib/utils/search_constants.ts @@ -63,6 +63,9 @@ export const SEARCH_LIMITS = { /** Batch size for profile fetching operations */ BATCH_SIZE: 50, + + /** Maximum events to fetch before processing in subscription search */ + SUBSCRIPTION_FETCH_LIMIT: 1000, } as const; // Nostr event kind ranges diff --git a/src/lib/utils/subscription_search.ts b/src/lib/utils/subscription_search.ts index b0c33d8..814fc63 100644 --- a/src/lib/utils/subscription_search.ts +++ b/src/lib/utils/subscription_search.ts @@ -809,7 +809,17 @@ function processPrimaryRelayResults( "events from primary relay", ); + // AI-NOTE: Apply subscription fetch limit to primary relay results + const maxEvents = SEARCH_LIMITS.SUBSCRIPTION_FETCH_LIMIT; + let processedCount = 0; + for (const event of events) { + // Check if we've reached the event limit + if (processedCount >= maxEvents) { + console.log(`subscription_search: Reached event limit of ${maxEvents} in primary relay processing`); + break; + } + // Check for abort signal if (abortSignal?.aborted) { cleanup?.(); @@ -827,6 +837,7 @@ function processPrimaryRelayResults( } else { processContentEvent(event, searchType, searchState); } + processedCount++; } catch (e) { console.warn("subscription_search: Error processing event:", e); // Invalid JSON or other error, skip @@ -834,7 +845,7 @@ function processPrimaryRelayResults( } console.log( - "subscription_search: Processed events - firstOrder:", + `subscription_search: Processed ${processedCount} events (limit: ${maxEvents}) - firstOrder:`, searchState.firstOrderEvents.length, "profiles:", searchState.foundProfiles.length, @@ -1011,7 +1022,20 @@ function searchOtherRelaysInBackground( callbacks.onSubscriptionCreated(sub); } + // AI-NOTE: Track event count to enforce subscription fetch limit + let eventCount = 0; + const maxEvents = SEARCH_LIMITS.SUBSCRIPTION_FETCH_LIMIT; + sub.on("event", (event: NDKEvent) => { + // Check if we've reached the event limit + if (eventCount >= maxEvents) { + console.log(`subscription_search: Reached event limit of ${maxEvents}, stopping event processing`); + sub.stop(); + return; + } + + eventCount++; + try { if (searchType === "n") { processProfileEvent(