stats: fix race conditions (on Postgres)

This commit is contained in:
Alex Gleason 2024-05-24 20:11:22 -05:00
parent 250998405a
commit 04018015c5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 19 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import { SetRequired } from 'type-fest';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { getTagSet } from '@/utils/tags.ts'; import { getTagSet } from '@/utils/tags.ts';
import { Conf } from '@/config.ts';
interface UpdateStatsOpts { interface UpdateStatsOpts {
kysely: Kysely<DittoTables>; kysely: Kysely<DittoTables>;
@ -153,8 +154,16 @@ export async function updateAuthorStats(
notes_count: 0, notes_count: 0,
}; };
const prev = await getAuthorStats(kysely, pubkey); let query = kysely
.selectFrom('author_stats')
.selectAll()
.where('pubkey', '=', pubkey);
if (Conf.db.dialect === 'postgres') {
query = query.forUpdate();
}
const prev = await query.executeTakeFirst();
const stats = fn(prev ?? empty); const stats = fn(prev ?? empty);
if (prev) { if (prev) {
@ -195,8 +204,16 @@ export async function updateEventStats(
reactions: '{}', reactions: '{}',
}; };
const prev = await getEventStats(kysely, eventId); let query = kysely
.selectFrom('event_stats')
.selectAll()
.where('event_id', '=', eventId);
if (Conf.db.dialect === 'postgres') {
query = query.forUpdate();
}
const prev = await query.executeTakeFirst();
const stats = fn(prev ?? empty); const stats = fn(prev ?? empty);
if (prev) { if (prev) {