pipeline: fix race condition in encounterEvent

This commit is contained in:
Alex Gleason 2024-05-25 12:13:55 -05:00
parent 4111d8b3cb
commit 6684edaeaf
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 11 additions and 13 deletions

View File

@ -3,12 +3,14 @@ import { LNURL } from '@nostrify/nostrify/ln';
import { PipePolicy } from '@nostrify/nostrify/policies';
import Debug from '@soapbox/stickynotes/debug';
import { sql } from 'kysely';
import { LRUCache } from 'lru-cache';
import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { DVM } from '@/pipeline/DVM.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { RelayError } from '@/RelayError.ts';
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
import { Storages } from '@/storages.ts';
@ -23,8 +25,6 @@ import { nip05Cache } from '@/utils/nip05.ts';
import { updateStats } from '@/utils/stats.ts';
import { getTagSet } from '@/utils/tags.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
const debug = Debug('ditto:pipeline');
/**
@ -33,7 +33,7 @@ const debug = Debug('ditto:pipeline');
*/
async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void> {
if (!(await verifyEventWorker(event))) return;
if (await encounterEvent(event, signal)) return;
if (encounterEvent(event)) return;
debug(`NostrEvent<${event.kind}> ${event.id}`);
if (event.kind !== 24133) {
@ -90,17 +90,15 @@ async function policyFilter(event: NostrEvent): Promise<void> {
}
}
const encounters = new LRUCache<string, true>({ max: 1000 });
/** Encounter the event, and return whether it has already been encountered. */
async function encounterEvent(event: NostrEvent, signal: AbortSignal): Promise<boolean> {
const cache = await Storages.cache();
const reqmeister = await Storages.reqmeister();
const [existing] = await cache.query([{ ids: [event.id], limit: 1 }]);
cache.event(event);
reqmeister.event(event, { signal });
return !!existing;
function encounterEvent(event: NostrEvent): boolean {
const encountered = !!encounters.get(event.id);
if (!encountered) {
encounters.set(event.id, true);
}
return encountered;
}
/** Hydrate the event with the user, if applicable. */