UNPKG

redis-semaphore

Version:

Distributed mutex and semaphore based on Redis

41 lines 1.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ioredis_1 = __importDefault(require("ioredis")); const Lock_1 = require("./Lock"); const acquire_1 = require("./mutex/acquire"); const refresh_1 = require("./mutex/refresh"); const release_1 = require("./mutex/release"); class RedisMutex extends Lock_1.Lock { constructor(client, key, options) { super(options); this._kind = 'mutex'; if (!client) { throw new Error('"client" is required'); } if (!(client instanceof ioredis_1.default)) { throw new Error('"client" must be instance of ioredis client or cluster'); } if (!key) { throw new Error('"key" is required'); } if (typeof key !== 'string') { throw new Error('"key" must be a string'); } this._client = client; this._key = `mutex:${key}`; } async _refresh() { return await (0, refresh_1.refreshMutex)(this._client, this._key, this._identifier, this._acquireOptions.lockTimeout); } async _acquire() { return await (0, acquire_1.acquireMutex)(this._client, this._key, this._acquireOptions); } async _release() { await (0, release_1.releaseMutex)(this._client, this._key, this._identifier); } } exports.default = RedisMutex; //# sourceMappingURL=RedisMutex.js.map