{"_id":"@async-cancellables/async-hybrid-limit","_rev":"1-fc7b47a0255de1df83fdc1be3e20afdc","name":"@async-cancellables/async-hybrid-limit","dist-tags":{"latest":"2.1.0"},"versions":{"2.0.0":{"name":"@async-cancellables/async-hybrid-limit","version":"2.0.0","description":"Asynchronous lock pool with priority and cancellation support","main":"index.js","type":"module","repository":{"type":"git","url":"git+https://github.com/vuwuv/async-cancellables.git","directory":"packages/async-hybrid-limit"},"keywords":["async","cancellable","AsyncHybridLimit","cancellation"],"author":{"name":"vuwuv"},"license":"MIT","bugs":{"url":"https://github.com/vuwuv/async-cancellables/issues"},"homepage":"https://github.com/vuwuv/async-cancellables#readme","peerDependencies":{"@async-cancellables/ct":"^2.0.0"},"engines":{"node":">=18.0.0"},"gitHead":"cca86a13c78c2c9eab5806f11d8f7a298f20aec0","_id":"@async-cancellables/async-hybrid-limit@2.0.0","_nodeVersion":"18.13.0","_npmVersion":"9.3.1","dist":{"integrity":"sha512-s9ninu4/pfUHYbu7mf4moMbzPW9HS3XOmzCAsftcYH6AUvxe6qI0F0+vMLp0Riah+bLsLXsr3AGXfJ16QI+DWw==","shasum":"1832467f68473703524ea5ddb369a3a46bd758ba","tarball":"https://registry.npmjs.org/@async-cancellables/async-hybrid-limit/-/async-hybrid-limit-2.0.0.tgz","fileCount":3,"unpackedSize":17098,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDyf9m0y/aHc9gNqYG1aGSEllipzy6uyd+LVTgOznhgWwIhAPCtpOrrW2BRAOC1ihRMad0uTHSvLw4mLh+FdBu8DTMl"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkBGTHACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmoo5w/+MpXM8x+nsaQ6dXnRJLeG7zMzOLxf4EDgof7em2A4+b9GiO/w\r\nMXO6bmZXlY9+hZPojRA8ubVsvR1+pWgpvTZ9hb3s4pIzaNQKKOhEoUzpBr+U\r\nzFrr7G3tmwMbhrUYYgFUMkq7OhB8VDS1tSRtDCJWNTGKuQpPC30xiY1V4Fpq\r\n0Ik5pmq4FR7QIVGYb97jcNd1LA0XTvFy/GFM7reFoEcReqMOwxd3DxLu9Dpo\r\n3jdq/+hNGbuPhSC4u8F6Rm+kJr0QEVURA1E3W2lnfUxDUvc39vwBnr9vmjdO\r\nhsbg6XQ8qQBddmmKKgyZj4fCtQa6JCo0zRenpIc9mYC6IZlzUl8noBiuPvaB\r\nMnMBVgOOol6dombtLBf5TvotFKE4wF7bw24yNvXlzhgR/gFg1ufv7pFrUcEW\r\na/4pP5wBARQFRd6hzRABrRTuDisy7f880nXQ3Da9FWZMkiVQCum3iBgUgAvx\r\nwmR93iFlEkyZiDAcWy8auVrZ6fHSfMz/l5K2m1zmGfcldkXbe33N/YN0u9Sb\r\nn/0BwB34nIncwvwaj5UlHfT3EPOY3SW5lkmq0yKn+I3DU7KqyunrhXW1mkru\r\nJsTU9FIeKiNFDXOCAGzUU+m2qGAykDQrQ/cC+cTsIr6iTlRqaWZhy3b/tqfj\r\nQhE1oXWsosXgfCDd+pCBLuMUL4skFB8hU4o=\r\n=iOmy\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"vuwuv","email":"vkhubb@gmail.com"},"directories":{},"maintainers":[{"name":"vuwuv","email":"vkhubb@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-hybrid-limit_2.0.0_1678009543652_0.24935651931978775"},"_hasShrinkwrap":false},"2.1.0":{"name":"@async-cancellables/async-hybrid-limit","version":"2.1.0","description":"Asynchronous lock pool with priority and cancellation support","main":"index.js","type":"module","repository":{"type":"git","url":"git+https://github.com/vuwuv/async-cancellables.git","directory":"packages/async-hybrid-limit"},"keywords":["async","cancellable","AsyncHybridLimit","cancellation"],"author":{"name":"vuwuv"},"license":"MIT","bugs":{"url":"https://github.com/vuwuv/async-cancellables/issues"},"homepage":"https://github.com/vuwuv/async-cancellables#readme","peerDependencies":{"@async-cancellables/ct":"^2.1.0"},"engines":{"node":">=18.0.0"},"gitHead":"286a1b8f801a432827adb845c47074d85aa338c6","_id":"@async-cancellables/async-hybrid-limit@2.1.0","_nodeVersion":"18.13.0","_npmVersion":"9.3.1","dist":{"integrity":"sha512-RJ212v3kFYRY+xDSoFYy5yD85rcHC6oFRHSfh/i6WqaqlIBulhUH+n1/pfc7w5YfEjRVxmx2eISFnFfR8lsu3g==","shasum":"e6ef45e5d2b73e53b2ad71f26f7174d06ece868f","tarball":"https://registry.npmjs.org/@async-cancellables/async-hybrid-limit/-/async-hybrid-limit-2.1.0.tgz","fileCount":3,"unpackedSize":17106,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC6rCt0ZQGj07gd5gGHlzpXmuwIOZRvUv97AZ8QSl4OPAIgRHr1jNcB5UzH69AZcNDx/APS0xCS/Q7X1s92r5IBsBU="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkB6WwACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo9chAAh6rHavf307y2xoWwT27bA4u9k8bUVWWnJGPPZI86pIlLnbrP\r\n5LDt5F6xQoOU/FKOYS2OtJw+tXkWqMCyHjF/s6wCs2ndRIBOrh7Hswwq0w2j\r\nkQh6pLd/TaKigdiodFhkKOzGBBrUAzE1TgHOMm0DMOs7bTGNn9bmBPPPdVqb\r\nuGwCkgns5Nkw+BKOLiSx65oYV/uESToVTA14PtwK4DrO78jWPKhgjXpYgkCm\r\nI+Ur3q2KbZ2kvlr+kxwfP/g7pWjVD8BAl3kLWYEhmRQafgwID/O7yyx7KHK9\r\nd5GZfZrdZ1qtxtSod9Pt7V9zCLR8yRnmbTll9FExNzhUxZgGxt6Z9BHjY3/r\r\niKPAo8iAF2yqlMmfbyo+Qezb7JmEHR8GFndDJc31RdapHS9m78T32F//jblw\r\nKpo9DNKuv46Yqo/cjsLQVyTVDoAcGx3+P3P0SPCi6AA3a9HVOGt+m3vQtmuz\r\nE40qYm69FX/Gezp4Fgo5gOArYSjBltyU5HM+KmqiKy3sCOmMwMSWehJlnKzi\r\nXOxXj48RcNkqwgpCq6xcVC1KrMLTfS9B7E7Ac/Lpb6UIc9wfuIILzy0J6Vhl\r\nZrF4lRZURizxdT01UJRZ1z4s3oALiIzAz7BjsnJALHeS/ZszXB+4dmnXOBag\r\nxeq+hrEtqWIQ6d2FmvN9Y1ykbUEp/VhuTKg=\r\n=N43x\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"vuwuv","email":"vkhubb@gmail.com"},"directories":{},"maintainers":[{"name":"vuwuv","email":"vkhubb@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-hybrid-limit_2.1.0_1678222768405_0.27553287116243763"},"_hasShrinkwrap":false}},"time":{"created":"2023-03-05T09:45:43.583Z","2.0.0":"2023-03-05T09:45:43.800Z","modified":"2023-03-07T20:59:28.661Z","2.1.0":"2023-03-07T20:59:28.549Z"},"maintainers":[{"name":"vuwuv","email":"vkhubb@gmail.com"}],"description":"Asynchronous lock pool with priority and cancellation support","homepage":"https://github.com/vuwuv/async-cancellables#readme","keywords":["async","cancellable","AsyncHybridLimit","cancellation"],"repository":{"type":"git","url":"git+https://github.com/vuwuv/async-cancellables.git","directory":"packages/async-hybrid-limit"},"author":{"name":"vuwuv"},"bugs":{"url":"https://github.com/vuwuv/async-cancellables/issues"},"license":"MIT","readme":"# Async Cancellables / AsyncHybridLimit\n\n> Asynchronous lock class allows to limit simultaneous resource access supporting prioritized access and cancellation tokens. Each slot request defines its own maximum slot count, which can be best described as each user having access to a different number of resource instances.\n\n## Table of contents\n\n- [Prerequisites](#prerequisites)\n- [Table of contents](#table-of-contents)\n- [Installation](#installation)\n- [Concept](#concept)\n- [Example](#example)\n- [API](#api)\n    - [Creating limit](#creating-limit)\n    - [Waiting slots](#waiting-slots)\n    - [Releasing slots](#releasing-slots)\n    - [Retrieving waiters and slots information](#retrieving-waiters-and-slots-information)\n- [Authors](#authors)\n- [License](#license)\n\n## Prerequisites\n\nThis project requires NodeJS (version 18 or later) and NPM.\n\n## Installation\n\nTo install and set up the library, run:\n\n```sh\n$ npm install @async-cancellables/async-hybrid-limit\n```\n\n## Concept\n\nHybrid limit is a lock with a dynamic number of slots. Each slot request defines its own maximum slot count, which can be best described as each user having access to a different number of resource instances. \n\nAssuming `wait(maxSlotCount, slotCount)` is a wait method:\n\n- if calling consequently `wait(3, 1)`, `wait(2, 1)` and `wait(1, 1)` all of them will be granted immediately\n- if calling consequently `wait(1, 1)`, `wait(3, 3)` the first one will be granted immediately and the second one will wait for the first one to release its slot\n\nSo the method tries to occupy the highest slots available: `wait(3, 1)` will try to occupy the third slot first, if it is not available, it will try to occupy the second slot and so on.\n\n## Example\n\nThe following example downloads a list of files while the number of concurrent downloads can be changed in the runtime (if it is decreased, the limit won't be applied to the already running downloads)\n\n```js\nimport AsyncHybridLimit from '@async-cancellables/async-hybrid-limit';\n\nlet changeableConcurrentDownloads = 5;\n\nasync downloadFile(limit, url) {\n    let ticket = await limit.waitOne(changeableConcurrentDownloads);\n    try {\n        ...\n    }\n    finally {\n        ticket.release();\n    }\n}\n\nconst limit = new AsyncHybridLimit();\nconst files = await Promise.all(urls.map(url => downloadFile(limit, url)));\n```\n\n## API\n\n### Creating limit\n\n`new AsyncHybridLimit()` creates new limit.\n\n### Waiting for slots\n\nThere are several wait methods:\n- `wait(maxSlotCount, slotCount, cancellationToken = null)` waits for `slotCount` slots with default priority of `0` assuming `maxSlotCount` total slots\n- `waitPrioritized(maxSlotCount, slotCount, priority, cancellationToken = null)` waits for `slotCount` slots with user defined (higher is sooner) priority assuming `maxSlotCount` total slots\n- `waitOne(maxSlotCount, cancellationToken = null)` waits for `1` slot with default priority of `0` assuming `maxSlotCount` total slots\n\nAll of them return `ticket` to be used for release. When using `cancellationToken` the waiter will be removed from the queue on cancellation.\n\n### Releasing slots\n\n- `release(ticket)` releases slots locked by any of the wait methods\n- `ticket.release()` releases slots locked by any of the wait methods\n\n### Getting waiters and slots information\n\n- `usedSlots(maxSlotCount)` get slot count currently used assuming `maxSlotCount` total slots\n- `freeSlots(maxSlotCount)` get slot count currently free assuming `maxSlotCount` total slots\n- `waitersPresent` returns `true` if there are waiters in the queue\n- `waitersCount` returns number of waiters in the queue\n- `waitersSlots` returns number of slots requested by waiters in the queue\n\n## Authors\n\n* **vuwuv** - *Initial work* - [vuwuv](https://github.com/vuwuv)\n\n## License\n\n[MIT License] © vuwuv","readmeFilename":"README.md"}