syncguard
Version:
Functional TypeScript library for distributed locking across microservices. Prevents race conditions with Redis, PostgreSQL, Firestore, and custom backends. Features automatic lock management, timeout handling, and extensible architecture.
28 lines (27 loc) • 1.33 kB
TypeScript
import type { Sql } from "postgres";
import { type LockOp, type ReleaseResult } from "../../common/backend.js";
import type { PostgresConfig } from "../types.js";
/**
* Creates PostgreSQL release operation with atomic transaction and ownership verification.
*
* **Implementation Pattern:**
* - Atomic transaction: Query by lockId → verify ownership → check liveness → delete
* - TOCTOU protection: All steps within single `sql.begin()` transaction
* - Ownership verification: Explicit `data.lock_id === opts.lockId` check (ADR-003)
* - AbortSignal: Manual cancellation checks via `checkAborted()` at strategic points
*
* Transaction flow:
* 1. Get server time for authoritative liveness check
* 2. Query by lockId using index (FOR UPDATE for row-level lock)
* 3. Verify ownership (data.lock_id === lockId)
* 4. Check liveness using isLive() predicate
* 5. If all checks pass, delete the lock
* 6. Return simplified result with optional telemetry reason
*
* @param sql - postgres.js SQL instance
* @param config - PostgreSQL backend configuration
* @returns Release operation function
*
* @see docs/specs/interface.md#release-operation-requirements - Normative TOCTOU requirements
*/
export declare function createReleaseOperation(sql: Sql, config: PostgresConfig): (opts: LockOp) => Promise<ReleaseResult>;