Browse Source

limit search size

master
silberengel 7 months ago
parent
commit
58185d29ca
  1. 3
      src/lib/utils/search_constants.ts
  2. 26
      src/lib/utils/subscription_search.ts

3
src/lib/utils/search_constants.ts

@ -63,6 +63,9 @@ export const SEARCH_LIMITS = {
/** Batch size for profile fetching operations */ /** Batch size for profile fetching operations */
BATCH_SIZE: 50, BATCH_SIZE: 50,
/** Maximum events to fetch before processing in subscription search */
SUBSCRIPTION_FETCH_LIMIT: 1000,
} as const; } as const;
// Nostr event kind ranges // Nostr event kind ranges

26
src/lib/utils/subscription_search.ts

@ -809,7 +809,17 @@ function processPrimaryRelayResults(
"events from primary relay", "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) { 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 // Check for abort signal
if (abortSignal?.aborted) { if (abortSignal?.aborted) {
cleanup?.(); cleanup?.();
@ -827,6 +837,7 @@ function processPrimaryRelayResults(
} else { } else {
processContentEvent(event, searchType, searchState); processContentEvent(event, searchType, searchState);
} }
processedCount++;
} catch (e) { } catch (e) {
console.warn("subscription_search: Error processing event:", e); console.warn("subscription_search: Error processing event:", e);
// Invalid JSON or other error, skip // Invalid JSON or other error, skip
@ -834,7 +845,7 @@ function processPrimaryRelayResults(
} }
console.log( console.log(
"subscription_search: Processed events - firstOrder:", `subscription_search: Processed ${processedCount} events (limit: ${maxEvents}) - firstOrder:`,
searchState.firstOrderEvents.length, searchState.firstOrderEvents.length,
"profiles:", "profiles:",
searchState.foundProfiles.length, searchState.foundProfiles.length,
@ -1011,7 +1022,20 @@ function searchOtherRelaysInBackground(
callbacks.onSubscriptionCreated(sub); 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) => { 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 { try {
if (searchType === "n") { if (searchType === "n") {
processProfileEvent( processProfileEvent(

Loading…
Cancel
Save