UNPKG

redis-semaphore

Version:

Distributed mutex and semaphore based on Redis

24 lines 1.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.refreshRedlockMultiSemaphore = void 0; const debug_1 = __importDefault(require("debug")); const lua_1 = require("../multiSemaphore/refresh/lua"); const redlock_1 = require("../utils/redlock"); const debug = (0, debug_1.default)('redis-semaphore:redlock-semaphore:refresh'); async function refreshRedlockMultiSemaphore(clients, key, limit, permits, options) { const { identifier, lockTimeout } = options; const now = Date.now(); debug(key, identifier, now); const quorum = (0, redlock_1.getQuorum)(clients.length); const promises = clients.map(client => (0, lua_1.refreshLua)(client, [key, limit, permits, identifier, lockTimeout, now]) .then(result => +result) .catch(() => 0)); const results = await Promise.all(promises); debug('results', results); return results.reduce(redlock_1.smartSum, 0) >= quorum; } exports.refreshRedlockMultiSemaphore = refreshRedlockMultiSemaphore; //# sourceMappingURL=refresh.js.map