client: use the same pool as the firehose

This commit is contained in:
Alex Gleason 2023-09-06 01:18:07 -05:00
parent 17c75e6761
commit 5f82f4f11b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 4 additions and 35 deletions

View File

@ -1,32 +1,9 @@
import { Conf } from '@/config.ts'; import { type Event, type Filter, matchFilters } from '@/deps.ts';
import { type Event, type Filter, matchFilters, RelayPool, TTLCache } from '@/deps.ts';
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { Time } from '@/utils.ts'; import { allRelays, pool } from '@/pool.ts';
import type { GetFiltersOpts } from '@/filter.ts'; import type { GetFiltersOpts } from '@/filter.ts';
type Pool = InstanceType<typeof RelayPool>;
/** HACK: Websockets in Deno are finnicky... get a new pool every 30 minutes. */
const poolCache = new TTLCache<0, Pool>({
ttl: Time.minutes(30),
max: 2,
dispose: (pool) => {
console.log('Closing pool.');
pool.close();
},
});
function getPool(): Pool {
const cached = poolCache.get(0);
if (cached !== undefined) return cached;
console.log('Creating new pool.');
const pool = new RelayPool(Conf.poolRelays);
poolCache.set(0, pool);
return pool;
}
/** Get events from a NIP-01 filter. */ /** Get events from a NIP-01 filter. */
function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts = {}): Promise<Event<K>[]> { function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts = {}): Promise<Event<K>[]> {
if (!filters.length) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]);
@ -34,9 +11,9 @@ function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts
let tid: number; let tid: number;
const results: Event[] = []; const results: Event[] = [];
const unsub = getPool().subscribe( const unsub = pool.subscribe(
filters, filters,
Conf.poolRelays, allRelays,
(event: Event | null) => { (event: Event | null) => {
if (event && matchFilters(filters, event)) { if (event && matchFilters(filters, event)) {
pipeline.handleEvent(event).catch(() => {}); pipeline.handleEvent(event).catch(() => {});

View File

@ -58,14 +58,6 @@ const Conf = {
get adminEmail() { get adminEmail() {
return Deno.env.get('ADMIN_EMAIL') || 'webmaster@localhost'; return Deno.env.get('ADMIN_EMAIL') || 'webmaster@localhost';
}, },
/** @deprecated Use relays from the database instead. */
get poolRelays() {
return (Deno.env.get('RELAY_POOL') || '').split(',').filter(Boolean);
},
/** @deprecated Publish only to the local relay unless users are mentioned, then try to also send to the relay of those users. Deletions should also be fanned out. */
get publishRelays() {
return ['wss://relay.mostr.pub'];
},
/** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */ /** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */
get url() { get url() {
return new URL(Conf.localDomain); return new URL(Conf.localDomain);