2023-04-06 21:37:40 +00:00
|
|
|
import { v4 as uuid } from "uuid";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { Connection, RawReqFilter, Nips } from "@snort/nostr";
|
2023-05-18 15:51:21 +00:00
|
|
|
import { unixNowMs, unwrap } from "Util";
|
2023-04-06 21:37:40 +00:00
|
|
|
import { NoteStore } from "./NoteCollection";
|
|
|
|
/**
|
|
|
|
* Tracing for relay query status
|
|
|
|
*/
|
|
|
|
class QueryTrace {
|
|
|
|
readonly id: string;
|
|
|
|
readonly subId: string;
|
|
|
|
readonly relay: string;
|
|
|
|
readonly connId: string;
|
|
|
|
readonly start: number;
|
|
|
|
sent?: number;
|
|
|
|
eose?: number;
|
|
|
|
close?: number;
|
|
|
|
#wasForceClosed = false;
|
|
|
|
readonly #fnClose: (id: string) => void;
|
|
|
|
|
|
|
|
constructor(sub: string, relay: string, connId: string, fnClose: (id: string) => void) {
|
|
|
|
this.id = uuid();
|
|
|
|
this.subId = sub;
|
|
|
|
this.relay = relay;
|
|
|
|
this.connId = connId;
|
|
|
|
this.start = unixNowMs();
|
|
|
|
this.#fnClose = fnClose;
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
|
2023-04-06 21:37:40 +00:00
|
|
|
sentToRelay() {
|
|
|
|
this.sent = unixNowMs();
|
|
|
|
}
|
|
|
|
|
|
|
|
gotEose() {
|
|
|
|
this.eose = unixNowMs();
|
2023-05-18 15:51:21 +00:00
|
|
|
if (this.responseTime > 5_000) {
|
|
|
|
console.debug(`Slow query ${this.subId} on ${this.relay} took ${this.responseTime.toLocaleString()}ms`);
|
|
|
|
}
|
2023-04-06 21:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forceEose() {
|
|
|
|
this.eose = unixNowMs();
|
|
|
|
this.#wasForceClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendClose() {
|
|
|
|
this.close = unixNowMs();
|
|
|
|
this.#fnClose(this.subId);
|
|
|
|
}
|
|
|
|
|
|
|
|
log() {
|
|
|
|
console.debug(
|
|
|
|
`QT:${this.id}, ${this.relay}, ${this.subId}, finished=${
|
|
|
|
this.finished
|
|
|
|
}, queued=${this.queued.toLocaleString()}ms, runtime=${this.runtime?.toLocaleString()}ms`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time spent in queue
|
|
|
|
*/
|
|
|
|
get queued() {
|
|
|
|
return (this.sent === undefined ? unixNowMs() : this.sent) - this.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Active or queued query on the system
|
|
|
|
*/
|
|
|
|
export class Query {
|
|
|
|
/**
|
|
|
|
* Uniquie ID of this query
|
|
|
|
*/
|
|
|
|
id: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The query payload (REQ filters)
|
|
|
|
*/
|
2023-04-06 21:37:40 +00:00
|
|
|
filters: Array<RawReqFilter>;
|
2023-03-28 14:34:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sub-Queries which are connected to this subscription
|
|
|
|
*/
|
|
|
|
subQueries: Array<Query> = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
leaveOpen = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of relays to send this query to
|
|
|
|
*/
|
|
|
|
relays: Array<string> = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time when this query can be removed
|
|
|
|
*/
|
|
|
|
#cancelTimeout?: number;
|
|
|
|
|
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
|
|
|
|
2023-04-25 17:01:29 +00:00
|
|
|
constructor(id: string, filters: Array<RawReqFilter>, feed: NoteStore) {
|
2023-03-28 14:34:01 +00:00
|
|
|
this.id = id;
|
2023-04-06 21:37:40 +00:00
|
|
|
this.filters = filters;
|
|
|
|
this.#feed = feed;
|
|
|
|
this.#checkTraces();
|
2023-03-28 14:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get closing() {
|
|
|
|
return this.#cancelTimeout !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
get closingAt() {
|
|
|
|
return this.#cancelTimeout;
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:37:40 +00:00
|
|
|
get feed() {
|
|
|
|
return this.#feed;
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
cancel() {
|
|
|
|
this.#cancelTimeout = unixNowMs() + 5_000;
|
|
|
|
}
|
|
|
|
|
|
|
|
unCancel() {
|
|
|
|
this.#cancelTimeout = undefined;
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:37:40 +00:00
|
|
|
cleanup() {
|
|
|
|
this.#stopCheckTraces();
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
sendToRelay(c: Connection) {
|
|
|
|
if (this.relays.length > 0 && !this.relays.includes(c.Address)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.relays.length === 0 && c.Ephemeral) {
|
|
|
|
console.debug("Cant send non-specific REQ to ephemeral connection");
|
|
|
|
return;
|
|
|
|
}
|
2023-04-06 21:37:40 +00:00
|
|
|
if (this.filters.some(a => a.search) && !c.SupportsNip(Nips.Search)) {
|
2023-03-28 14:34:01 +00:00
|
|
|
console.debug("Cant send REQ to non-search relay", c.Address);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-06 21:37:40 +00:00
|
|
|
const qt = new QueryTrace(this.id, c.Address, c.Id, x => c.CloseReq(x));
|
|
|
|
this.#tracing.push(qt);
|
|
|
|
c.QueueReq(["REQ", this.id, ...this.filters], () => qt.sentToRelay());
|
|
|
|
}
|
|
|
|
|
|
|
|
connectionLost(c: Connection, active: Array<string>, pending: Array<string>) {
|
|
|
|
const allQueriesLost = [...active, ...pending].filter(a => this.id === a || this.subQueries.some(b => b.id === a));
|
|
|
|
if (allQueriesLost.length > 0) {
|
|
|
|
console.debug("Lost", allQueriesLost, c.Address, c.Id);
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendClose() {
|
2023-04-06 21:37:40 +00:00
|
|
|
for (const qt of this.#tracing) {
|
|
|
|
qt.sendClose();
|
2023-03-28 14:34:01 +00:00
|
|
|
}
|
2023-03-29 14:17:12 +00:00
|
|
|
for (const sq of this.subQueries) {
|
|
|
|
sq.sendClose();
|
|
|
|
}
|
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-04-25 17:01:29 +00:00
|
|
|
const qt = this.#tracing.find(a => a.subId === sub && a.connId === conn.Id);
|
|
|
|
qt?.gotEose();
|
2023-03-28 15:41:57 +00:00
|
|
|
if (sub === this.id) {
|
2023-04-06 21:37:40 +00:00
|
|
|
console.debug(`[EOSE][${sub}] ${conn.Address}`);
|
2023-05-18 15:51:21 +00:00
|
|
|
if (!this.leaveOpen) {
|
|
|
|
qt?.sendClose();
|
2023-04-06 21:37:40 +00:00
|
|
|
}
|
2023-03-28 15:41:57 +00:00
|
|
|
} else {
|
|
|
|
const subQ = this.subQueries.find(a => a.id === sub);
|
|
|
|
if (subQ) {
|
2023-04-06 21:37:40 +00:00
|
|
|
subQ.eose(sub, conn);
|
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-04-06 21:37:40 +00:00
|
|
|
let 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)) {
|
|
|
|
thisProgress = 0;
|
|
|
|
}
|
2023-03-28 15:41:57 +00:00
|
|
|
if (this.subQueries.length === 0) {
|
|
|
|
return thisProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalProgress = thisProgress;
|
|
|
|
for (const sq of this.subQueries) {
|
|
|
|
totalProgress += sq.progress;
|
|
|
|
}
|
|
|
|
return totalProgress / (this.subQueries.length + 1);
|
|
|
|
}
|
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) {
|
|
|
|
//v.log();
|
|
|
|
if (v.runtime > 5_000 && !v.finished) {
|
|
|
|
v.forceEose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 2_000);
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
}
|