1
0
forked from Kieran/snort
snort/packages/system/src/Query.ts

294 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-04-06 21:37:40 +00:00
import { v4 as uuid } from "uuid";
import debug from "debug";
2023-06-15 11:03:05 +00:00
import { unixNowMs, unwrap } from "@snort/shared";
2023-06-08 10:45:23 +00:00
import { Connection, ReqFilter, Nips, TaggedRawEvent } from ".";
2023-06-15 11:03:05 +00:00
import { reqFilterEq } from "./Utils";
2023-04-06 21:37:40 +00:00
import { NoteStore } from "./NoteCollection";
2023-06-08 10:45:23 +00:00
import { flatMerge } from "./RequestMerger";
2023-06-01 08:54:25 +00:00
import { BuiltRawReqFilter } from "./RequestBuilder";
2023-06-08 04:39:10 +00:00
import { expandFilter } from "./RequestExpander";
2023-04-06 21:37:40 +00:00
/**
* Tracing for relay query status
*/
class QueryTrace {
readonly id: string;
readonly start: number;
sent?: number;
eose?: number;
close?: number;
#wasForceClosed = false;
readonly #fnClose: (id: string) => void;
2023-05-24 16:17:17 +00:00
readonly #fnProgress: () => void;
2023-05-29 21:25:40 +00:00
constructor(
readonly relay: string,
readonly filters: Array<ReqFilter>,
2023-05-29 21:25:40 +00:00
readonly connId: string,
fnClose: (id: string) => void,
fnProgress: () => void
) {
2023-04-06 21:37:40 +00:00
this.id = uuid();
this.start = unixNowMs();
this.#fnClose = fnClose;
2023-05-24 16:17:17 +00:00
this.#fnProgress = fnProgress;
2023-04-06 21:37:40 +00:00
}
2023-03-28 14:34:01 +00:00
2023-04-06 21:37:40 +00:00
sentToRelay() {
this.sent = unixNowMs();
2023-05-24 16:17:17 +00:00
this.#fnProgress();
2023-04-06 21:37:40 +00:00
}
gotEose() {
this.eose = unixNowMs();
2023-05-24 16:17:17 +00:00
this.#fnProgress();
2023-04-06 21:37:40 +00:00
}
forceEose() {
this.eose = unixNowMs();
this.#wasForceClosed = true;
this.sendClose();
2023-04-06 21:37:40 +00:00
}
sendClose() {
this.close = unixNowMs();
2023-06-01 08:54:25 +00:00
this.#fnClose(this.id);
2023-05-24 16:17:17 +00:00
this.#fnProgress();
2023-04-06 21:37:40 +00:00
}
/**
* Time spent in queue
*/
get queued() {
2023-05-24 16:17:17 +00:00
return (this.sent === undefined ? unixNowMs() : this.#wasForceClosed ? unwrap(this.eose) : this.sent) - this.start;
2023-04-06 21:37:40 +00:00
}
/**
* Total query runtime
*/
get runtime() {
return (this.eose === undefined ? unixNowMs() : this.eose) - this.start;
}
2023-05-18 15:51:21 +00:00
/**
* Total time spent waiting for relay to respond
*/
get responseTime() {
return this.finished ? unwrap(this.eose) - unwrap(this.sent) : 0;
}
2023-04-06 21:37:40 +00:00
/**
* If tracing is finished, we got EOSE or timeout
*/
get finished() {
return this.eose !== undefined;
}
2023-03-28 14:34:01 +00:00
}
2023-05-24 16:17:17 +00:00
export interface QueryBase {
2023-03-28 14:34:01 +00:00
/**
* Uniquie ID of this query
*/
id: string;
/**
* The query payload (REQ filters)
*/
filters: Array<ReqFilter>;
2023-03-28 14:34:01 +00:00
/**
2023-05-24 16:17:17 +00:00
* List of relays to send this query to
2023-03-28 14:34:01 +00:00
*/
2023-05-24 16:17:17 +00:00
relays?: Array<string>;
}
/**
* Active or queued query on the system
*/
2023-05-29 21:25:40 +00:00
export class Query implements QueryBase {
/**
* Uniquie ID of this query
*/
2023-05-24 16:17:17 +00:00
id: string;
2023-06-13 15:29:51 +00:00
/**
* RequestBuilder instance
*/
fromInstance: string;
2023-03-28 14:34:01 +00:00
/**
* Which relays this query has already been executed on
*/
2023-04-06 21:37:40 +00:00
#tracing: Array<QueryTrace> = [];
2023-03-28 15:41:57 +00:00
2023-03-28 14:34:01 +00:00
/**
* Leave the query open until its removed
*/
2023-06-08 05:27:27 +00:00
#leaveOpen = false;
2023-03-28 14:34:01 +00:00
/**
* Time when this query can be removed
*/
2023-06-08 05:27:27 +00:00
#cancelAt?: number;
2023-03-28 14:34:01 +00:00
2023-04-06 21:37:40 +00:00
/**
* Timer used to track tracing status
*/
#checkTrace?: ReturnType<typeof setInterval>;
/**
* Feed object which collects events
*/
2023-04-25 17:01:29 +00:00
#feed: NoteStore;
2023-04-06 21:37:40 +00:00
#log = debug("Query");
2023-05-24 16:17:17 +00:00
2023-06-13 15:29:51 +00:00
constructor(id: string, instance: string, feed: NoteStore, leaveOpen?: boolean) {
2023-03-28 14:34:01 +00:00
this.id = id;
2023-04-06 21:37:40 +00:00
this.#feed = feed;
2023-06-13 15:29:51 +00:00
this.fromInstance = instance;
2023-06-08 05:27:27 +00:00
this.#leaveOpen = leaveOpen ?? false;
2023-04-06 21:37:40 +00:00
this.#checkTraces();
2023-03-28 14:34:01 +00:00
}
2023-06-08 05:27:27 +00:00
canRemove() {
return this.#cancelAt !== undefined && this.#cancelAt < unixNowMs();
2023-03-28 14:34:01 +00:00
}
2023-06-08 04:39:10 +00:00
/**
* Recompute the complete set of compressed filters from all query traces
*/
2023-06-01 08:54:25 +00:00
get filters() {
2023-06-12 13:15:45 +00:00
return flatMerge(this.flatFilters);
}
get flatFilters() {
2023-06-14 01:00:14 +00:00
const f: Array<ReqFilter> = [];
for (const x of this.#tracing.flatMap(a => a.filters)) {
if (!f.some(a => reqFilterEq(a, x))) {
f.push(x);
}
}
return f.flatMap(expandFilter);
2023-06-01 08:54:25 +00:00
}
2023-04-06 21:37:40 +00:00
get feed() {
return this.#feed;
}
2023-06-01 08:54:25 +00:00
onEvent(sub: string, e: TaggedRawEvent) {
for (const t of this.#tracing) {
if (t.id === sub) {
this.feed.add(e);
break;
}
}
2023-05-29 21:25:40 +00:00
}
2023-06-08 05:27:27 +00:00
/**
* This function should be called when this Query object and FeedStore is no longer needed
*/
2023-03-28 14:34:01 +00:00
cancel() {
2023-06-08 05:27:27 +00:00
this.#cancelAt = unixNowMs() + 5_000;
}
uncancel() {
this.#cancelAt = undefined;
2023-03-28 14:34:01 +00:00
}
2023-04-06 21:37:40 +00:00
cleanup() {
this.#stopCheckTraces();
}
2023-06-01 08:54:25 +00:00
sendToRelay(c: Connection, subq: BuiltRawReqFilter) {
if (!this.#canSendQuery(c, subq)) {
2023-03-28 14:34:01 +00:00
return;
}
2023-06-01 08:54:25 +00:00
return this.#sendQueryInternal(c, subq);
2023-04-06 21:37:40 +00:00
}
connectionLost(id: string) {
this.#tracing.filter(a => a.connId == id).forEach(a => a.forceEose());
2023-03-28 14:34:01 +00:00
}
sendClose() {
2023-05-24 16:17:17 +00:00
for (const qt of this.#tracing) {
qt.sendClose();
2023-03-29 14:17:12 +00:00
}
2023-04-06 21:37:40 +00:00
this.cleanup();
2023-03-28 14:34:01 +00:00
}
2023-03-28 15:41:57 +00:00
2023-04-06 21:37:40 +00:00
eose(sub: string, conn: Readonly<Connection>) {
2023-06-01 08:54:25 +00:00
const qt = this.#tracing.find(a => a.id === sub && a.connId === conn.Id);
2023-04-25 17:01:29 +00:00
qt?.gotEose();
2023-06-08 05:27:27 +00:00
if (!this.#leaveOpen) {
qt?.sendClose();
}
2023-03-28 15:41:57 +00:00
}
/**
* Get the progress to EOSE, can be used to determine when we should load more content
*/
get progress() {
2023-05-24 16:17:17 +00:00
const thisProgress = this.#tracing.reduce((acc, v) => (acc += v.finished ? 1 : 0), 0) / this.#tracing.length;
2023-03-29 10:40:05 +00:00
if (isNaN(thisProgress)) {
2023-05-24 16:17:17 +00:00
return 0;
2023-03-28 15:41:57 +00:00
}
2023-05-24 16:17:17 +00:00
return thisProgress;
}
2023-03-28 15:41:57 +00:00
2023-05-24 16:17:17 +00:00
#onProgress() {
const isFinished = this.progress === 1;
if (this.feed.loading !== isFinished) {
this.#log("%s loading=%s, progress=%d", this.id, this.feed.loading, this.progress);
2023-05-24 16:17:17 +00:00
this.feed.loading = isFinished;
2023-03-28 15:41:57 +00:00
}
}
2023-04-06 21:37:40 +00:00
#stopCheckTraces() {
if (this.#checkTrace) {
clearInterval(this.#checkTrace);
}
}
#checkTraces() {
this.#stopCheckTraces();
this.#checkTrace = setInterval(() => {
for (const v of this.#tracing) {
if (v.runtime > 5_000 && !v.finished) {
v.forceEose();
}
}
2023-05-24 16:17:17 +00:00
}, 500);
}
2023-06-01 08:54:25 +00:00
#canSendQuery(c: Connection, q: BuiltRawReqFilter) {
if (q.relay && q.relay !== c.Address) {
2023-05-24 16:17:17 +00:00
return false;
}
2023-06-01 08:54:25 +00:00
if (!q.relay && c.Ephemeral) {
this.#log("Cant send non-specific REQ to ephemeral connection %O %O %O", q, q.relay, c);
2023-05-24 16:17:17 +00:00
return false;
}
if (q.filters.some(a => a.search) && !c.SupportsNip(Nips.Search)) {
this.#log("Cant send REQ to non-search relay", c.Address);
2023-05-24 16:17:17 +00:00
return false;
}
return true;
}
2023-06-01 08:54:25 +00:00
#sendQueryInternal(c: Connection, q: BuiltRawReqFilter) {
2023-05-24 16:17:17 +00:00
const qt = new QueryTrace(
c.Address,
2023-05-29 21:25:40 +00:00
q.filters,
2023-05-24 16:17:17 +00:00
c.Id,
x => c.CloseReq(x),
() => this.#onProgress()
);
this.#tracing.push(qt);
2023-06-01 08:54:25 +00:00
c.QueueReq(["REQ", qt.id, ...q.filters], () => qt.sentToRelay());
return qt;
2023-04-06 21:37:40 +00:00
}
2023-03-28 14:34:01 +00:00
}