UNPKG

@shagital/atomic-lock

Version:

Universal atomic locking with pluggable drivers (Redis, SQLite, File, Memory)

30 lines (29 loc) 1.28 kB
import { LockOptions, DriverConfig, CircuitBreakerStats } from '../types'; /** * Universal atomic lock with memory-safe circuit breaker */ export declare class AtomicLock { /** * Callback-style lock usage. Acquires the lock, runs the callback, always releases the lock. * Throws if lock cannot be acquired. Returns the callback's result. */ withLock<T>(key: string, callback: (lockValue: string) => Promise<T> | T, options?: LockOptions): Promise<T>; private driver; private lockFailures; private readonly circuitBreakerThreshold; private readonly circuitBreakerResetTime; private readonly maxFailureEntries; private lastCleanup; constructor(config: DriverConfig, options?: LockOptions); private createDriver; tryAcquire(key: string, options?: LockOptions): Promise<string | null>; acquire(key: string, options?: LockOptions): Promise<string>; release(key: string, lockValue: string): Promise<boolean>; getCircuitBreakerStatus(key: string): CircuitBreakerStats; private isCircuitOpen; private recordLockFailure; private cleanupFailureRecords; private generateLockValue; close(): Promise<void>; } export declare function createLock(config: DriverConfig, options?: LockOptions): AtomicLock;