Files
mtusi2026lab4/src/Attachment/object-lock.ts
T
qrtx dc46ac213d feat(storage): add RustFS attachments and presigned uploads
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
2026-11-12 10:16:39 +03:00

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;
}