forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
17 lines
704 B
TypeScript
17 lines
704 B
TypeScript
import { Prisma } from '../generated/prisma/client';
|
|
|
|
export const OBJECT_TRANSACTION = { maxWait: 5000, timeout: 20000 };
|
|
|
|
/** All writers and cleaners use the same transaction-scoped lock for an immutable key. */
|
|
export async function tryAttachmentObjectLock(
|
|
tx: Prisma.TransactionClient,
|
|
objectKey: string,
|
|
): Promise<boolean> {
|
|
// A suspended/crashed writer must not keep an idle transaction open indefinitely.
|
|
await tx.$executeRaw`SET LOCAL idle_in_transaction_session_timeout = '15s'`;
|
|
const [result] = await tx.$queryRaw<{ locked: boolean }[]>(Prisma.sql`
|
|
SELECT pg_try_advisory_xact_lock(hashtextextended(${`attachment:${objectKey}`}, 0)) AS locked
|
|
`);
|
|
return result.locked;
|
|
}
|