Giter Club home page Giter Club logo

Comments (4)

swarthy avatar swarthy commented on September 25, 2024 1

MultiSemaphore was added in v3.2.0

from redis-semaphore.

swarthy avatar swarthy commented on September 25, 2024

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 Semaphores and call acquire for each of them, I guess. What do you think? Thanks

from redis-semaphore.

Sytten avatar Sytten commented on September 25, 2024

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.

Sytten avatar Sytten commented on September 25, 2024

Wow so fast, thank you so much, this is awesome!

from redis-semaphore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.