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.

39 lines 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DistributedReleaser = void 0; class DistributedReleaser { constructor(onRelease, token) { this.onRelease = onRelease; this.token = token; this.isReleased = false; } /** * Releasing is idempotent: one releaser owns exactly one acquisition, so * repeated calls must not unlock again (or hand a semaphore permit back twice). * * A failed `onRelease` does not stick the releaser in a released state, so * the caller can retry after a transient Redis/network error. */ async release() { if (this.isReleased) { return; } if (!this.releasePromise) { this.releasePromise = (async () => { try { await this.onRelease(); this.isReleased = true; } finally { this.releasePromise = undefined; } })(); } return this.releasePromise; } getToken() { return this.token; } } exports.DistributedReleaser = DistributedReleaser; //# sourceMappingURL=distributed-releaser.js.map