use LRUSet to skip already seen event processing

This commit is contained in:
Martti Malmi
2024-01-04 10:36:23 +02:00
parent c2899eac26
commit 287ce32690
5 changed files with 21 additions and 1 deletions

View File

@ -9,6 +9,8 @@ import { ConnectionStats } from "./connection-stats";
import { NostrEvent, ReqCommand, ReqFilter, TaggedNostrEvent, u256 } from "./nostr";
import { RelayInfo } from "./relay-info";
import EventKind from "./event-kind";
import seenEvents from "./seen-events";
import {getHex64} from "./utils";
/**
* Relay settings
@ -199,6 +201,14 @@ export class Connection extends EventEmitter<ConnectionEvents> {
OnMessage(e: WebSocket.MessageEvent) {
this.#activity = unixNowMs();
if ((e.data as string).length > 0) {
// skip message processing if we've already seen it
const msgId = getHex64(e.data as string, "id");
if (seenEvents.has(msgId)) {
console.log('already seen');
return;
}
seenEvents.add(msgId); // TODO only do after msg validation
const msg = JSON.parse(e.data as string) as Array<string | NostrEvent | boolean>;
const tag = msg[0] as string;
switch (tag) {