feat: upgrade caches to worker

This commit is contained in:
2024-01-17 15:47:01 +00:00
parent 3c808688f8
commit aa58ec4185
32 changed files with 698 additions and 417 deletions

View File

@ -1,10 +1,10 @@
import debug from "debug";
import { FeedCache, removeUndefined } from "@snort/shared";
import { CachedTable, removeUndefined } from "@snort/shared";
import { SystemInterface, TaggedNostrEvent, RequestBuilder } from ".";
export abstract class BackgroundLoader<T extends { loaded: number; created: number }> {
#system: SystemInterface;
readonly cache: FeedCache<T>;
readonly cache: CachedTable<T>;
#log = debug(this.name());
/**
@ -17,7 +17,7 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
*/
loaderFn?: (pubkeys: Array<string>) => Promise<Array<T>>;
constructor(system: SystemInterface, cache: FeedCache<T>) {
constructor(system: SystemInterface, cache: CachedTable<T>) {
this.#system = system;
this.cache = cache;
this.#FetchMetadata();

View File

@ -5,7 +5,7 @@ import { ProfileLoaderService } from "./profile-cache";
import { RelayCache, RelayMetadataLoader } from "./outbox-model";
import { Optimizer } from "./query-optimizer";
import { base64 } from "@scure/base";
import { FeedCache } from "@snort/shared";
import { CachedTable } from "@snort/shared";
import { ConnectionPool } from "./connection-pool";
import EventEmitter from "eventemitter3";
import { QueryEvents } from "./query";
@ -143,7 +143,7 @@ export interface SystemInterface {
/**
* Generic cache store for events
*/
get eventsCache(): FeedCache<NostrEvent>;
get eventsCache(): CachedTable<NostrEvent>;
/**
* Relay loader loads relay metadata for a set of profiles

View File

@ -1,8 +1,8 @@
import debug from "debug";
import EventEmitter from "eventemitter3";
import { FeedCache } from "@snort/shared";
import { NostrEvent, ReqFilter, TaggedNostrEvent } from "./nostr";
import { CachedTable } from "@snort/shared";
import { NostrEvent, TaggedNostrEvent } from "./nostr";
import { RelaySettings, ConnectionStateSnapshot, OkResponse } from "./connection";
import { BuiltRawReqFilter, RequestBuilder } from "./request-builder";
import { RelayMetricHandler } from "./relay-metric-handler";
@ -17,7 +17,6 @@ import {
RelayMetricCache,
UsersRelays,
SnortSystemDb,
EventExt,
QueryLike,
} from ".";
import { EventsCache } from "./cache/events";
@ -34,10 +33,10 @@ export interface NostrSystemEvents {
}
export interface NostrsystemProps {
relayCache?: FeedCache<UsersRelays>;
profileCache?: FeedCache<CachedMetadata>;
relayMetrics?: FeedCache<RelayMetrics>;
eventsCache?: FeedCache<NostrEvent>;
relayCache?: CachedTable<UsersRelays>;
profileCache?: CachedTable<CachedMetadata>;
relayMetrics?: CachedTable<RelayMetrics>;
eventsCache?: CachedTable<NostrEvent>;
optimizer?: Optimizer;
db?: SnortSystemDb;
checkSigs?: boolean;
@ -53,17 +52,17 @@ export class NostrSystem extends EventEmitter<NostrSystemEvents> implements Syst
/**
* Storage class for user relay lists
*/
readonly relayCache: FeedCache<UsersRelays>;
readonly relayCache: CachedTable<UsersRelays>;
/**
* Storage class for user profiles
*/
readonly profileCache: FeedCache<CachedMetadata>;
readonly profileCache: CachedTable<CachedMetadata>;
/**
* Storage class for relay metrics (connects/disconnects)
*/
readonly relayMetricsCache: FeedCache<RelayMetrics>;
readonly relayMetricsCache: CachedTable<RelayMetrics>;
/**
* Profile loading service
@ -81,7 +80,7 @@ export class NostrSystem extends EventEmitter<NostrSystemEvents> implements Syst
readonly optimizer: Optimizer;
readonly pool: ConnectionPool;
readonly eventsCache: FeedCache<NostrEvent>;
readonly eventsCache: CachedTable<NostrEvent>;
readonly relayLoader: RelayMetadataLoader;
/**

View File

@ -1,11 +1,11 @@
import { FeedCache, unixNowMs } from "@snort/shared";
import { CachedTable, unixNowMs } from "@snort/shared";
import { RelayMetrics } from "./cache";
import { TraceReport } from "./query";
export class RelayMetricHandler {
readonly #cache: FeedCache<RelayMetrics>;
readonly #cache: CachedTable<RelayMetrics>;
constructor(cache: FeedCache<RelayMetrics>) {
constructor(cache: CachedTable<RelayMetrics>) {
this.#cache = cache;
setInterval(() => {

View File

@ -21,7 +21,7 @@ import {
} from "..";
import { NostrSystemEvents, NostrsystemProps } from "../nostr-system";
import { WorkerCommand, WorkerMessage } from ".";
import { FeedCache } from "@snort/shared";
import { CachedTable } from "@snort/shared";
import { EventsCache } from "../cache/events";
import { RelayMetricHandler } from "../relay-metric-handler";
import debug from "debug";
@ -31,12 +31,12 @@ export class SystemWorker extends EventEmitter<NostrSystemEvents> implements Sys
#log = debug("SystemWorker");
#worker: Worker;
#commandQueue: Map<string, (v: unknown) => void> = new Map();
readonly relayCache: FeedCache<UsersRelays>;
readonly profileCache: FeedCache<CachedMetadata>;
readonly relayMetricsCache: FeedCache<RelayMetrics>;
readonly relayCache: CachedTable<UsersRelays>;
readonly profileCache: CachedTable<CachedMetadata>;
readonly relayMetricsCache: CachedTable<RelayMetrics>;
readonly profileLoader: ProfileLoaderService;
readonly relayMetricsHandler: RelayMetricHandler;
readonly eventsCache: FeedCache<NostrEvent>;
readonly eventsCache: CachedTable<NostrEvent>;
readonly relayLoader: RelayMetadataLoader;
get checkSigs() {