redis-semaphore icon indicating copy to clipboard operation
redis-semaphore copied to clipboard

Behavior changed for `externallyAcquiredIdentifier` after 5.3.1

Open AngelMsger opened this issue 2 years ago • 0 comments

Hi, thank you for your awesome lib!

I used to use the externallyAcquiredIdentifier option of Mutex to implement a reentrant distributed lock. That's to say, if multi locks have the same identifier, they will be treated as the same lock holder. It is convenient because there are many complex scenarios I don't want to add a lock state argument to all my functions involved and pass through everywhere. Instead, I can use transaction id as an identifier to indicate that if the mutex has been acquired by the outer stack frame, just go through. This is an example test case:

test('reentrant', async ({ assert }) => {
    const identifier = randomUUID();
    const lock1 = new Mutex(redis, key, {
        // ...,
        externallyAcquiredIdentifier: identifier
    });
    const lock2 = new Mutex(redis, key, {
        // ...,
        externallyAcquiredIdentifier: identifier
    });
    assert.isTrue(await lock1.tryAcquire());
    assert.isTrue(await lock2.tryAcquire());
});

But it was broken at version 5.3.1 it this commit: https://github.com/swarthy/redis-semaphore/commit/3f704899b4f2dcd4e28233948cfe2b0c59bd07ee#diff-f30ff58349492ea4098fb2a9d1e13e5ea42c34b003959dc4d1e8a008bfcdf313

Is there any alternative or suggestion?

AngelMsger avatar Aug 22 '23 03:08 AngelMsger