{"id":13565702,"url":"https://github.com/rogierschouten/async-lock","last_synced_at":"2025-04-03T23:30:30.002Z","repository":{"id":38686954,"uuid":"64934765","full_name":"rogierschouten/async-lock","owner":"rogierschouten","description":"Lock on asynchronous code for Nodejs","archived":false,"fork":true,"pushed_at":"2024-09-21T19:31:07.000Z","size":246,"stargazers_count":397,"open_issues_count":6,"forks_count":50,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-04T19:42:05.863Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"rain1017/async-lock","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rogierschouten.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-04T12:57:40.000Z","updated_at":"2024-11-03T15:29:12.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/rogierschouten/async-lock","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rogierschouten%2Fasync-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rogierschouten%2Fasync-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rogierschouten%2Fasync-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rogierschouten%2Fasync-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rogierschouten","download_url":"https://codeload.github.com/rogierschouten/async-lock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247097557,"owners_count":20883121,"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-01T13:01:53.505Z","updated_at":"2025-04-03T23:30:29.731Z","avatar_url":"https://github.com/rogierschouten.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# async-lock\n\nLock on asynchronous code\n\n[![Build Status](https://travis-ci.org/rogierschouten/async-lock.svg?branch=master)](https://travis-ci.org/rogierschouten/async-lock)\n\n* ES6 promise supported\n* Multiple keys lock supported\n* Timeout supported\n* Occupation time limit supported\n* Execution time limit supported\n* Pending task limit supported\n* Domain reentrant supported\n* 100% code coverage\n\n## Disclaimer\n\nI did not create this package. I was granted the ownership because it was no longer being maintained, and I volunteered to fix a bug.\nI will not do any maintenance on it other than merge PRs.\n\n**DO NOT EXPECT ME TO ADD FEATURES OR FIX BUGS.  PRs WELCOME**\n\n\n## Why do you need locking on single threaded nodejs?\n\nNodejs is single threaded, and the code execution never gets interrupted inside an event loop, so locking is unnecessary? This is true ONLY IF your critical section can be executed inside a single event loop.\nHowever, if you have any async code inside your critical section (it can be simply triggered by any I/O operation, or timer), your critical logic will across multiple event loops, therefore it's not concurrency safe!\n\nConsider the following code\n```js\nredis.get('key', function(err, value) {\n\tredis.set('key', value * 2);\n});\n```\nThe above code simply multiply a redis key by 2.\nHowever, if two users run concurrently, the execution order may like this\n```\nuser1: redis.get('key') -\u003e 1\nuser2: redis.get('key') -\u003e 1\nuser1: redis.set('key', 1 x 2) -\u003e 2\nuser2: redis.set('key', 1 x 2) -\u003e 2\n```\nObviously it's not what you expected\n\n\nWith asyncLock, you can easily write your async critical section\n```js\nlock.acquire('key', function(cb) {\n\t// Concurrency safe\n\tredis.get('key', function(err, value) {\n\t\tredis.set('key', value * 2, cb);\n\t});\n}, function(err, ret) {\n});\n```\n\n## Get Started\n\n```js\nvar AsyncLock = require('async-lock');\nvar lock = new AsyncLock();\n\n/**\n * @param {String|Array} key \tresource key or keys to lock\n * @param {function} fn \texecute function\n * @param {function} cb \t(optional) callback function, otherwise will return a promise\n * @param {Object} opts \t(optional) options\n */\nlock.acquire(key, function(done) {\n\t// async work\n\tdone(err, ret);\n}, function(err, ret) {\n\t// lock released\n}, opts);\n\n// Promise mode\nlock.acquire(key, function() {\n\t// return value or promise\n}, opts).then(function() {\n\t// lock released\n});\n```\n\n## Error Handling\n\n```js\n// Callback mode\nlock.acquire(key, function(done) {\n\tdone(new Error('error'));\n}, function(err, ret) {\n\tconsole.log(err.message) // output: error\n});\n\n// Promise mode\nlock.acquire(key, function() {\n\tthrow new Error('error');\n}).catch(function(err) {\n\tconsole.log(err.message) // output: error\n});\n```\n\n## Acquire multiple keys\n\n```js\nlock.acquire([key1, key2], fn, cb);\n```\n\n## Domain reentrant lock\n\nLock is reentrant in the same domain\n\n```js\nvar domain = require('domain');\nvar lock = new AsyncLock({domainReentrant : true});\n\nvar d = domain.create();\nd.run(function() {\n\tlock.acquire('key', function() {\n\t\t//Enter lock\n\t\treturn lock.acquire('key', function() {\n\t\t\t//Enter same lock twice\n\t\t});\n\t});\n});\n```\n\n## Options\n\n```js\n// Specify timeout - max amount of time an item can remain in the queue before acquiring the lock\nvar lock = new AsyncLock({timeout: 5000});\nlock.acquire(key, fn, function(err, ret) {\n\t// timed out error will be returned here if lock not acquired in given time\n});\n\n// Specify max occupation time - max amount of time allowed between entering the queue and completing execution\nvar lock = new AsyncLock({maxOccupationTime: 3000});\nlock.acquire(key, fn, function(err, ret) {\n\t// occupation time exceeded error will be returned here if job not completed in given time\n});\n\n// Specify max execution time - max amount of time allowed between acquiring the lock and completing execution\nvar lock = new AsyncLock({maxExecutionTime: 3000});\nlock.acquire(key, fn, function(err, ret) {\n\t// execution time exceeded error will be returned here if job not completed in given time\n});\n\n// Set max pending tasks - max number of tasks allowed in the queue at a time\nvar lock = new AsyncLock({maxPending: 1000});\nlock.acquire(key, fn, function(err, ret) {\n\t// Handle too much pending error\n})\n\n// Whether there is any running or pending async function\nlock.isBusy();\n\n// Use your own promise library instead of the global Promise variable\nvar lock = new AsyncLock({Promise: require('bluebird')}); // Bluebird\nvar lock = new AsyncLock({Promise: require('q')}); // Q\n\n// Add a task to the front of the queue waiting for a given lock\nlock.acquire(key, fn1, cb); // runs immediately\nlock.acquire(key, fn2, cb); // added to queue\nlock.acquire(key, priorityFn, cb, {skipQueue: true}); // jumps queue and runs before fn2\n```\n\n## Changelog\n\nSee [Changelog](./History.md)\n\n## Issues\n\nSee [issue tracker](https://github.com/rogierschouten/async-lock/issues).\n\n## License\n\nMIT, see [LICENSE](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frogierschouten%2Fasync-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frogierschouten%2Fasync-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frogierschouten%2Fasync-lock/lists"}