Try wrapping slow queries in transaction

This commit is contained in:
Alex Gleason 2023-10-11 19:37:52 -05:00
parent fe99e4a053
commit 121de85a84
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 19 additions and 12 deletions

View file

@ -1,3 +1,4 @@
import { db } from '@/db.ts';
import { z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts';
import * as mixer from '@/mixer.ts';
@ -40,8 +41,10 @@ async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
return c.json([]);
}
return db.transaction().execute(async () => {
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));
return paginated(c, events, statuses);
})
}
export { hashtagTimelineController, homeTimelineController, publicTimelineController };

View file

@ -1,3 +1,4 @@
import { db } from '@/db.ts';
import { Conf } from '@/config.ts';
import * as eventsDB from '@/db/events.ts';
import { addRelays } from '@/db/relays.ts';
@ -21,6 +22,8 @@ import type { EventData } from '@/types.ts';
*/
async function handleEvent(event: Event): Promise<void> {
if (encounterEvent(event)) return;
await db.transaction().execute(async () => {
const data = await getEventData(event);
await Promise.all([
@ -32,6 +35,7 @@ async function handleEvent(event: Event): Promise<void> {
streamOut(event, data),
broadcast(event, data),
]);
})
}
/** Tracks encountered events to skip duplicates, improving idempotency and performance. */