Comments (4)
MultiSemaphore
was added in v3.2.0
from redis-semaphore.
Hi!
Adding int argument to acquire
is not quite correct, because there are several ways how to make multiple lock. For your example, lets say there are serveral running instances of application which should connect to service (some of them needs 1 lock to open single connection, other needs 2). Instance1 locks two slots, Instance2 locks one slot, after that there are one free slot left. What should instance3 do? Waits until two slots become available (possibly may never happend, cause there are instance2 which can always lock "third" slot)? Or should it lock one slot and waits until other one will be released? If second UX is what you want, then you can do it with current interface: create two instances of Semaphore
with same key and call await Promise.all([semaphore1.acquire(), semaphore2.acquire()])
, which is more clear I guess. (offtop: calling .acquire
on single semaphore instance multiple times doesn't locks multiple slots, because internall semaphore based on sorted set in redis, so only one slot for single semaphore identifier will be "relocked")
For that UX it's better to create something like "MultiSemaphore" class with acquire
which will internally create multiple Semaphore
s and call acquire
for each of them, I guess. What do you think? Thanks
from redis-semaphore.
So i believe that instance3
here should throw on the acquire if it requires 2 but only 1 slot is available. That really depends if you hold locks for a long time or if it is a temporary lock. I think that should be left to the application developer.
A MultiSemaphore
class could work, but I would expect it to be atomic and in the case of using multiple classes of Semaphore
under it, it won't be.
I feel this script https://github.com/swarthy/redis-semaphore/blob/master/src/semaphore/acquire/lua.ts#L14-L18
could be adapted to to do something like (not a lua programmer, so probably wrong but you get the gist):
if redis.call('zcard', key) + ask <= limit then
array = {}
for i= 0, ask do
array[i*2] = now
array[i+1] = identifier
end
redis.call('zadd', key, unpack(array))
Thanks!
from redis-semaphore.
Wow so fast, thank you so much, this is awesome!
from redis-semaphore.
Related Issues (20)
- Execution environment built with Webpack throws error for `NOSCRIPT No matching script. Please use EVAL.` HOT 4
- Non-throwing acquire
- this._acquired is not checked on release HOT 1
- Export TimeoutError class
- Support for fences
- Breaking changes HOT 1
- Doesn't seem to work from version 3.1.0 - what am I missing? HOT 2
- Support for max request per seconds in semaphore HOT 2
- Mutex silently reacquires lock when expired between refreshes HOT 2
- redis cluster is not supported for Semaphore HOT 3
- Behavior changed for `externallyAcquiredIdentifier` after 5.3.1 HOT 1
- Connection timeout during refresh throws an unhandled error that shuts down node 16+ HOT 3
- Explain `refreshInterval` HOT 2
- Support for ioredis-mock HOT 4
- Losing locks in distributed system HOT 1
- Redlock-mutex is timing out with local redis cluster HOT 1
- Lost mutex when computer comes out of standby mode HOT 4
- Node-redis support HOT 6
- [MultiSemaphore] ReplyError: ERR Error running script: @user_script:18: user_script:18: too many results to unpack HOT 2
- Does this library support Redis Cluster? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redis-semaphore.