Git Product home page Git Product logo

Comments (3)

mike-marcacci avatar mike-marcacci commented on July 26, 2024 3

Hi @dvergeylen – So sorry for the slow reply here!

So, the library expects an actual instance of Lock for two reasons.

  1. The method is actually supposed to mutate its expiration to immediately invalidate it. This prevents accidentally extending a lock you are attempting to invalidate.
  2. More importantly, the lock value is what prevents a dangerous race condition. Imagine you have 2 servers, and server A gets a lock on SOME_RESOURCE. Eventually, server A is done with the resource, and sends a request to redis to release the lock on SOME_RESOURCE. Now, due to the asynchronous nature of network requests, it's possible that the lock expires before the request reaches and is processed by redis. It's also possible that in that time, server B is able to successfully acquire a NEW lock on SOME_RESOURCE. If only the resource name is used in the eventually-processed "unlock" request, then it would be possible for it to prematurely release the new lock acquired by server B! To prevent this, each lock has a cryptographically random value, to make it unique. This way, the "unlock" request from server A will only succeed if the currently-held lock is the one it's attempting to release.

All that to say, you'll almost certainly want to track at least the lock's resource and value, if you aren't able to keep a reference to the original lock.

To unlock the resource, the "most correct" way would be:

const Redlock = require('redlock');

const savedLockResource = 'SOME RESOURCE';
const savedLockValue = 'xxxxxx';

lock = new Redlock.Lock(redlock, savedLockResource, savedLockValue, 1);
lock.unlock();

More concisely, you could get away with:

redlock.unlock({ resource: 'SOME_RESOURCE', value: 'xxxxxx' });

Hope that helps!

from node-redlock.

dvergeylen avatar dvergeylen commented on July 26, 2024

I could find a workaround 😸. But I am still interested if you have an elegant way to retrieve a lock from its resource name.

from node-redlock.

dvergeylen avatar dvergeylen commented on July 26, 2024

Hope that helps!

It does! Thank you @mike-marcacci ! 👍

from node-redlock.

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.