{"id":13526773,"url":"https://github.com/TheDeveloper/warlock","last_synced_at":"2025-04-01T08:30:18.562Z","repository":{"id":14771753,"uuid":"17493319","full_name":"TheDeveloper/warlock","owner":"TheDeveloper","description":"Battle-hardened distributed locking using redis","archived":false,"fork":false,"pushed_at":"2023-03-05T23:29:32.000Z","size":96,"stargazers_count":146,"open_issues_count":6,"forks_count":23,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T19:08:23.950Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheDeveloper.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-03-06T21:30:17.000Z","updated_at":"2024-12-26T14:20:32.000Z","dependencies_parsed_at":"2024-01-13T22:25:31.473Z","dependency_job_id":"094b097a-c0b0-429f-b1ea-63c8d2453cc2","html_url":"https://github.com/TheDeveloper/warlock","commit_stats":{"total_commits":64,"total_committers":10,"mean_commits":6.4,"dds":0.421875,"last_synced_commit":"a5ee7bd7ab709a7174c1db0e5ad409c80c4046c8"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDeveloper%2Fwarlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDeveloper%2Fwarlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDeveloper%2Fwarlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDeveloper%2Fwarlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheDeveloper","download_url":"https://codeload.github.com/TheDeveloper/warlock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246607035,"owners_count":20804508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:34.556Z","updated_at":"2025-04-01T08:30:18.304Z","avatar_url":"https://github.com/TheDeveloper.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"warlock\n=======\n\n[![Join the chat at https://gitter.im/TheDeveloper/warlock](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/TheDeveloper/warlock?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nBattle-hardened distributed locking using redis.\n\n## Requirements\n\n* [node-redis](https://github.com/mranney/node_redis) compatible with `v0.10`\n* Redis `v2.6.12` or above. If you're running a Redis version from `v2.6.0` to `v2.6.11` inclusive use `v0.0.7` of this module.\n\n## Install\n\n    npm install node-redis-warlock\n\n## Usage\n\n```javascript\nconst Warlock = require('node-redis-warlock');\nconst Redis = require('redis');\n\n// Establish a redis client and pass it to warlock\nconst redis = Redis.createClient();\nconst warlock = Warlock(redis);\n\n// Set a lock\nconst key = 'test-lock';\nconst ttl = 10000; // Lifetime of the lock\n\nwarlock.lock(key, ttl, (err, unlock) =\u003e {\n  if (err) {\n    // Something went wrong and we weren't able to set a lock\n    return;\n  }\n\n  if (typeof unlock === 'function') {\n    // If the lock is set successfully by this process, an unlock function is passed to our callback.\n    // Do the work that required lock protection, and then unlock() when finished...\n    //\n    // do stuff...\n    //\n    unlock();\n  } else {\n    // Otherwise, the lock was not established by us so we must decide what to do\n    // Perhaps wait a bit \u0026 retry...\n  }\n});\n\n// set a lock optimistically\nconst key = 'opt-lock';\nconst ttl = 10000;\nconst maxAttempts = 4; // Max number of times to try setting the lock before erroring\nconst wait = 1000; // Time to wait before another attempt if lock already in place\nwarlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) =\u003e {});\n\n// unlock using the lock id\nvar key = 'test-lock-2';\nvar ttl = 10000;\nlet lockId;\n\nwarlock.lock(key, ttl, (err, _, id) =\u003e {\n  lockId = id;\n});\n\n// each client who knows the lockId can release the lock\nwarlock.unlock(key, lockId, (err, result) =\u003e {\n  if (result == 1) {\n    // unlocked successfully\n  }\n});\n\n// change a lock's ttl\nvar key = 'touch-lock';\nvar ttl = 10000;\nvar ttl2 = 20000;\n\nwarlock.lock(key, ttl, function(err, unlock, id) {\n  warlock.touch(key, id, ttl2, function(err) {});\n});\n```\n\n## ProTips\n\n* Read my [Distributed locks using Redis](https://engineering.gosquared.com/distributed-locks-using-redis) article and Redis' author's [A proposal for more reliable locks using Redis](http://antirez.com/news/77) to learn more about the theory of distributed locks using Redis.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTheDeveloper%2Fwarlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTheDeveloper%2Fwarlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTheDeveloper%2Fwarlock/lists"}