use LRUSet to skip already seen event processing
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
c2899eac26
commit
287ce32690
@ -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;
|
||||
|
@ -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) {
|
||||
|
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;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user