use LRUSet to skip already seen event processing
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import Dexie, { Table } from "dexie";
|
import Dexie, { Table } from "dexie";
|
||||||
import { TaggedNostrEvent, ReqFilter as Filter } from "@snort/system";
|
import { TaggedNostrEvent, ReqFilter as Filter } from "@snort/system";
|
||||||
import * as Comlink from "comlink";
|
import * as Comlink from "comlink";
|
||||||
import LRUSet from "@/Cache/LRUSet";
|
import LRUSet from "@snort/shared/src/LRUSet";
|
||||||
|
|
||||||
type Tag = {
|
type Tag = {
|
||||||
id: string;
|
id: string;
|
||||||
|
@ -9,6 +9,8 @@ import { ConnectionStats } from "./connection-stats";
|
|||||||
import { NostrEvent, ReqCommand, ReqFilter, TaggedNostrEvent, u256 } from "./nostr";
|
import { NostrEvent, ReqCommand, ReqFilter, TaggedNostrEvent, u256 } from "./nostr";
|
||||||
import { RelayInfo } from "./relay-info";
|
import { RelayInfo } from "./relay-info";
|
||||||
import EventKind from "./event-kind";
|
import EventKind from "./event-kind";
|
||||||
|
import seenEvents from "./seen-events";
|
||||||
|
import {getHex64} from "./utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Relay settings
|
* Relay settings
|
||||||
@ -199,6 +201,14 @@ export class Connection extends EventEmitter<ConnectionEvents> {
|
|||||||
OnMessage(e: WebSocket.MessageEvent) {
|
OnMessage(e: WebSocket.MessageEvent) {
|
||||||
this.#activity = unixNowMs();
|
this.#activity = unixNowMs();
|
||||||
if ((e.data as string).length > 0) {
|
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 msg = JSON.parse(e.data as string) as Array<string | NostrEvent | boolean>;
|
||||||
const tag = msg[0] as string;
|
const tag = msg[0] as string;
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
|
3
packages/system/src/seen-events.ts
Normal file
3
packages/system/src/seen-events.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import LRUSet from "@snort/shared/src/LRUSet";
|
||||||
|
|
||||||
|
export default new LRUSet<string>(2000);
|
@ -82,3 +82,10 @@ export function parseIMeta(tags: Array<Array<string>>) {
|
|||||||
}
|
}
|
||||||
return ret;
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user