Add an initial limit back to the relay

This commit is contained in:
Alex Gleason 2025-02-19 20:51:07 -06:00
parent 9401c0e013
commit aefa6bed6e
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 10 additions and 6 deletions

View file

@ -125,7 +125,7 @@ function connectStream(socket: WebSocket, ip: string | undefined, conf: DittoCon
const store = await Storages.db();
try {
for await (const [verb, , ...rest] of store.req(filters, { timeout: conf.db.timeouts.relay })) {
for await (const [verb, , ...rest] of store.req(filters, { limit: 100, timeout: conf.db.timeouts.relay })) {
send([verb, subId, ...rest] as NostrRelayMsg);
}
} catch (e) {

View file

@ -262,10 +262,10 @@ export class DittoPgStore extends NPostgres {
override async *req(
filters: NostrFilter[],
opts: { timeout?: number; signal?: AbortSignal } = {},
opts: { timeout?: number; signal?: AbortSignal; limit?: number } = {},
): AsyncIterable<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED> {
const { db, chunkSize = 20 } = this.opts;
const { timeout = this.opts.timeout, signal } = opts;
const { limit, timeout = this.opts.timeout, signal } = opts;
filters = await this.expandFilters(filters);
@ -273,11 +273,15 @@ export class DittoPgStore extends NPostgres {
const normalFilters = this.normalizeFilters(filters);
const machina = new Machina<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED>(signal);
if (normalFilters.length) {
if (normalFilters.length && limit !== 0) {
this.withTimeout(db.kysely as unknown as Kysely<NPostgresSchema>, timeout, async (trx) => {
const rows = this.getEventsQuery(trx, normalFilters).stream(chunkSize);
let query = this.getEventsQuery(trx, normalFilters);
for await (const row of rows) {
if (typeof opts.limit === 'number') {
query = query.limit(opts.limit);
}
for await (const row of query.stream(chunkSize)) {
const event = this.parseEventRow(row);
machina.push(['EVENT', subId, event]);
}