mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
pipeline: skip previously encountered events
This commit is contained in:
parent
a0dff12ca0
commit
2f7914f044
1 changed files with 12 additions and 1 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import * as eventsDB from '@/db/events.ts';
|
import * as eventsDB from '@/db/events.ts';
|
||||||
import { addRelays } from '@/db/relays.ts';
|
import { addRelays } from '@/db/relays.ts';
|
||||||
import { findUser } from '@/db/users.ts';
|
import { findUser } from '@/db/users.ts';
|
||||||
import { type Event } from '@/deps.ts';
|
import { type Event, LRUCache } from '@/deps.ts';
|
||||||
import { isLocallyFollowed } from '@/queries.ts';
|
import { isLocallyFollowed } from '@/queries.ts';
|
||||||
import { Sub } from '@/subs.ts';
|
import { Sub } from '@/subs.ts';
|
||||||
import { trends } from '@/trends.ts';
|
import { trends } from '@/trends.ts';
|
||||||
|
|
@ -14,6 +14,7 @@ import type { EventData } from '@/types.ts';
|
||||||
* It is idempotent, so it can be called multiple times for the same event.
|
* It is idempotent, so it can be called multiple times for the same event.
|
||||||
*/
|
*/
|
||||||
async function handleEvent(event: Event): Promise<void> {
|
async function handleEvent(event: Event): Promise<void> {
|
||||||
|
if (encounterEvent(event)) return;
|
||||||
const data = await getEventData(event);
|
const data = await getEventData(event);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
|
|
@ -24,6 +25,16 @@ async function handleEvent(event: Event): Promise<void> {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tracks encountered events to skip duplicates, improving idempotency and performance. */
|
||||||
|
const encounters = new LRUCache<string, boolean>({ max: 1000 });
|
||||||
|
|
||||||
|
/** Encounter the event, and return whether it has already been encountered. */
|
||||||
|
function encounterEvent(event: Event) {
|
||||||
|
const result = encounters.get(event.id);
|
||||||
|
encounters.set(event.id, true);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/** Preload data that will be useful to several tasks. */
|
/** Preload data that will be useful to several tasks. */
|
||||||
async function getEventData({ pubkey }: Event): Promise<EventData> {
|
async function getEventData({ pubkey }: Event): Promise<EventData> {
|
||||||
const user = await findUser({ pubkey });
|
const user = await findUser({ pubkey });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue