UNPKG

@apiratorjs/locking-redis

Version:

An extension to the core @apiratorjs/locking library, providing Redis-based implementations of distributed mutexes and semaphores for true cross-process concurrency control in Node.js.

60 lines 2.3 kB
import { RedisClientType } from "redis"; import { ELockDisplayType, types } from "@apiratorjs/locking"; export type TRedisDistributedLockManagerProps = { redisClient: RedisClientType; }; /** * Redis-backed implementation of IDistributedLockManager. * * Owns a set of named distributed locks keyed in Redis: hands out the same * instance for the same name while that lock is alive, and knows what it handed * out so an application can list, cancel, or tear everything down on shutdown. * * ```ts * const locks = await RedisDistributedLockManager.create({ url: "redis://localhost:6379" }); * await locks.mutex("orders").runExclusive(() => shipOrder()); * * process.on("SIGTERM", async () => { * await locks.destroyAll("Shutting down"); * await locks.disconnect(); * }); * ``` */ export declare class RedisDistributedLockManager implements types.IDistributedLockManager { private readonly redisClient; private readonly managedLocks; private readonly ownsClient; constructor(props: TRedisDistributedLockManagerProps, options?: { ownsClient?: boolean; }); /** * Connects to Redis and returns a manager that owns the client lifecycle. */ static create(options: { url: string; }): Promise<RedisDistributedLockManager>; getRedisClient(): RedisClientType; /** * Disconnects the Redis client when this manager created it via {@link create}. * No-op when the client was injected from outside. */ disconnect(): Promise<void>; mutex(name: string): types.IDistributedMutex; semaphore(name: string, maxCount: number): types.IDistributedSemaphore; readWriteLock(name: string, maxReaders?: number): types.IDistributedRWLock; hasMutex(name: string): boolean; hasSemaphore(name: string): boolean; hasRWLock(name: string): boolean; list(): types.TDistributedLockInfo[]; count(kind?: ELockDisplayType): number; snapshot(): Promise<types.TDistributedLockSnapshot[]>; cancelAll(errMessage?: string): Promise<void>; destroyAll(errMessage?: string): Promise<void>; private cancelOne; private describe; private keyOf; private takeAlive; private dropDestroyed; private throwIfAnyFailed; } //# sourceMappingURL=redis-distributed-lock-manager.d.ts.map