UNPKG

redis-semaphore

Version:

Distributed mutex and semaphore based on Redis

37 lines 1.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.acquireSemaphore = void 0; const debug_1 = __importDefault(require("debug")); const index_1 = require("../../utils/index"); const lua_1 = require("./lua"); const debug = (0, debug_1.default)('redis-semaphore:semaphore:acquire'); async function acquireSemaphore(client, key, limit, options) { const { identifier, lockTimeout, acquireTimeout, retryInterval } = options; const end = Date.now() + acquireTimeout; let now; while ((now = Date.now()) < end) { debug(key, identifier, limit, lockTimeout); const result = await (0, lua_1.acquireLua)(client, [ key, limit, identifier, lockTimeout, now ]); debug(key, 'result', typeof result, result); // support options.stringNumbers if (+result === 1) { debug(key, identifier, 'acquired'); return true; } else { await (0, index_1.delay)(retryInterval); } } return false; } exports.acquireSemaphore = acquireSemaphore; //# sourceMappingURL=index.js.map