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

@ -1,7 +1,7 @@
import Dexie, { Table } from "dexie";
import { TaggedNostrEvent, ReqFilter as Filter } from "@snort/system";
import * as Comlink from "comlink";
import LRUSet from "@/Cache/LRUSet";
import LRUSet from "@snort/shared/src/LRUSet";
type Tag = {
id: string;

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) {

View File

@ -0,0 +1,3 @@
import LRUSet from "@snort/shared/src/LRUSet";
export default new LRUSet<string>(2000);

View File

@ -82,3 +82,10 @@ export function parseIMeta(tags: Array<Array<string>>) {
}
return ret;
}
export function getHex64(json: string, field: string): string {
let len = field.length + 3
let idx = json.indexOf(`"${field}":`) + len
let s = json.slice(idx).indexOf(`"`) + idx + 1
return json.slice(s, s + 64)
}