{"_id":"@bitfinex/bfx-facs-redis-lock","name":"@bitfinex/bfx-facs-redis-lock","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.2":{"name":"@bitfinex/bfx-facs-redis-lock","version":"0.0.2","private":false,"description":"Bitfinex Redis Lock Facility","author":{"name":"prdn","email":"paolo@bitfinex.com"},"keywords":["bitfinex"],"dependencies":{"@bitfinex/bfx-facs-base":"git+https://github.com/bitfinexcom/bfx-facs-base.git","async":"^3.2.1","lodash":"^4.17.21","redlock":"4.2.0"},"engines":{"node":">=16.0"},"main":"index.js","license":"Apache-2.0","repository":{"type":"git","url":"git+https://github.com/bitfinexcom/bfx-facs-redis-lock.git"},"homepage":"https://github.com/bitfinexcom/bfx-facs-base","maintainers":[{"name":"vigan-abd","email":"vigan.abd@gmail.com"},{"name":"bitfinexorg","email":"paolo@bitfinex.com"},{"name":"prdn","email":"paolo.ardoino@gmail.com"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_id":"@bitfinex/bfx-facs-redis-lock@0.0.2","gitHead":"7acf520c9d74b387bccde9cb81b7478087196bdd","bugs":{"url":"https://github.com/bitfinexcom/bfx-facs-redis-lock/issues"},"_nodeVersion":"18.20.4","_npmVersion":"10.7.0","dist":{"integrity":"sha512-hfQEPjSeUTp30zcZgyZK+CxfDo8iIc5WAmKtPYFcMByisQPrm4sq8JO2IvAJ24pkl2yeUArlZpKTy5IznKRF+w==","shasum":"9eac3a193d62286a33fc9916ba28b80d22600d71","tarball":"https://registry.npmjs.org/@bitfinex/bfx-facs-redis-lock/-/bfx-facs-redis-lock-0.0.2.tgz","fileCount":5,"unpackedSize":17316,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQD5o5b3c0BabZFDjNdlL1wQQju2Zy0AUJ13ohauoy1kWQIgIB3e0FnRfbfX5+/izxOv/WasGhR1T67S8tGVvQkrSus="}]},"_npmUser":{"name":"vigan-abd","email":"vigan.abd@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/bfx-facs-redis-lock_0.0.2_1769658413850_0.8985181295323477"},"_hasShrinkwrap":false}},"time":{"created":"2026-01-29T03:46:53.761Z","0.0.2":"2026-01-29T03:46:53.976Z","modified":"2026-01-29T03:46:54.229Z"},"maintainers":[{"name":"vigan-abd","email":"vigan.abd@gmail.com"},{"name":"bitfinexorg","email":"paolo@bitfinex.com"},{"name":"prdn","email":"paolo.ardoino@gmail.com"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"description":"Bitfinex Redis Lock Facility","homepage":"https://github.com/bitfinexcom/bfx-facs-base","keywords":["bitfinex"],"repository":{"type":"git","url":"git+https://github.com/bitfinexcom/bfx-facs-redis-lock.git"},"author":{"name":"prdn","email":"paolo@bitfinex.com"},"bugs":{"url":"https://github.com/bitfinexcom/bfx-facs-redis-lock/issues"},"license":"Apache-2.0","readme":"# bfx-facs-redis-lock\n\n## Development\n\n```\ngit clone https://github.com/bitfinexcom/bfx-facs-redis-lock\ncd bfx-facs-redis-lock\ngit remote add upstream https://github.com/bitfinexcom/bfx-facs-base\n\n```\n\n## Usage\n\nIn your service, run:\n\n```\nnpm i --save https://github.com/bitfinexcom/bfx-facs-redis-lock\n```\n\n### Integration\nRedisLock is a class that extends from Facility, the main class in bfx-facs-base.\n\nThis Facility encapsulates the **[node-redlock](https://github.com/mike-marcacci/node-redlock)** libary.\n\nRedlock can use node redis, ioredis redis library to keep its client connections.\n\n```js\n\n// Instance an object\nconst RedLockFac = require('bfx-facs-redis-lock')\n// Create a redis client (Redlock can use node redis, ioredis or any other compatible redis library to keep its client connections.)\nconst redisClient = require('redis').createClient(6379, 'redis1.example.com');\n// Pass redis client to redlock\nconst redLockFac = new RedLockFac (caller, {\n    redis_client : redisClient\n}, ctx)\n```\n\n\n```js\n\n// Within a worker constructor\n\nthis.setInitFacs([\n    // Start a Redis client First.\n    ['fac', 'bfx-facs-redis', '0', '0',{}, -4],\n    \n    ['fac', 'bfx-facs-redis-lock', '0', '0', () => {\n        // Pass a Redis client to Redis Lock Fac\n        return {\n            redis_client: this.redis_0.cli_rw\n        }\n    }, -3]\n])\n\n```\n## Main functions\n\n### `RedLockFac.lock.lock(resource, ttl, ?callback) => Promise<Lock>`\n- `resource (string)` resource to be locked\n- `ttl (number)` time in ms until the lock expires\n- `callback (function)` callback returning:\n\t- `err (Error)`\n\t- `lock (Lock)`\n\n\n### `RedLockFac.lock.unlock(lock, ?callback) => Promise`\n- `lock (Lock)` lock to be released\n- `callback (function)` callback returning:\n\t- `err (Error)`\n\n\n### `RedLockFac.lock.extend(lock, ttl, ?callback) => Promise<Lock>`\n- `lock (Lock)` lock to be extended\n- `ttl (number)` time in ms to extend the lock's expiration\n- `callback (function)` callback returning:\n\t- `err (Error)`\n\t- `lock (Lock)`\n\n\n### `RedLockFac.lock.disposer(resource, ttl, ?unlockErrorHandler)`\n- `resource (string)` resource to be locked\n- `ttl (number)` time in ms to extend the lock's expiration\n- `callback (function)` error handler called with:\n\t- `err (Error)`\n\n\n### `RedLockFac.lock.quit(?callback) => Promise<*[]>`\n- `callback (function)` error handler called with:\n\t- `err (Error)`\n\t- `*[]` results of calling `.quit()` on each client\n\n\n### `Lock.unlock(?callback) => Promise`\n- `callback (function)` callback returning:\n\t- `err (Error)`\n\n\n### `Lock.extend(ttl, ?callback) => Promise<Lock>`\n- `ttl (number)` time from now in ms to set as the lock's new expiration\n- `callback (function)` callback returning:\n\t- `err (Error)`\n\t- `lock (Lock)`\n\n\n\n## Usage\n### Locking & Unlocking (Callbacks)\n\n```js\n\n// the string identifier for the resource you want to lock\nvar resource = 'locks:account:322456';\n\n// the maximum amount of time you want the resource locked,\n// keeping in mind that you can extend the lock up until\n// the point when it expires\nvar ttl = 1000;\n\nredlock.lock(resource, ttl, function(err, lock) {\n\n\t// we failed to lock the resource\n\tif(err) {\n\t\t// ...\n\t}\n\n\t// we have the lock\n\telse {\n\n\n\t\t// ...do something here...\n\n\n\t\t// unlock your resource when you are done\n\t\tlock.unlock(function(err) {\n\t\t\t// we weren't able to reach redis; your lock will eventually\n\t\t\t// expire, but you probably want to log this error\n\t\t\tconsole.error(err);\n\t\t});\n\t}\n});\n\n```\n\n### Locking and Extending (Promises)\n\n```js\nredlock.lock('locks:account:322456', 1000).then(function(lock) {\n\n\t// ...do something here...\n\n\t// if you need more time, you can continue to extend\n\t// the lock as long as you never let it expire\n\n\t// this will extend the lock so that it expires\n\t// approximitely 1s from when `extend` is called\n\treturn lock.extend(1000).then(function(lock){\n\n\t\t// ...do something here...\n\n\t\t// unlock your resource when you are done\n\t\treturn lock.unlock()\n\t\t.catch(function(err) {\n\t\t\t// we weren't able to reach redis; your lock will eventually\n\t\t\t// expire, but you probably want to log this error\n\t\t\tconsole.error(err);\n\t\t});\n\t});\n});\n\n```\n","readmeFilename":"README.md","_rev":"1-0cd25218d43480357019ea542189603c"}