redislock icon indicating copy to clipboard operation
redislock copied to clipboard

How to get lock with a key

Open tshubham19 opened this issue 3 years ago • 1 comments

This is not an issue, just a question. How do we get lock having just a key value. We need this option in the scenarios when we acquire a lock in a thread and try to release the same lock in the other thread. Thanks team,

tshubham19 avatar May 16 '22 07:05 tshubham19

At the moment, you cannot, but could you just share the Lock instance across threads (protected by a mutex, obviously)? Happy to accept a PR where a Lock can be created from a key.

dim avatar May 17 '22 15:05 dim

I created a PR that should (hopefully) make it possible.

https://github.com/bsm/redislock/pull/51

janosmiko avatar Oct 21 '22 12:10 janosmiko

What If I want to release form another process ? I cannot share it. It would be super helpful in a replicated application environment

timo-klarshift avatar Apr 02 '23 17:04 timo-klarshift

@timo-klarshift you can use my fork.

Instead of the regular Obtain, call it like this:

// Try to obtain lock.
locker.Obtain(ctx, "my-key", 150*time.Millisecond, &redislock.Options{
	Secret: "test",
})

If the second application uses the same secret, it will be able to obtain (and release) the lock.

janosmiko avatar Apr 02 '23 17:04 janosmiko

@timo-klarshift @janosmiko I would be more than happy to incorporate your fork into our version, but I still don't understand how locking and releasing from separate processes can be beneficial. I can see many side-effects from having a shared Secret. Could you please explain the use case where this may be beneficial? Normally, you would want a distributed lock so that multiple processes don't perform the same task concurrently.

dim avatar Apr 03 '23 08:04 dim

I tried to explain my use case in the PR. But here's a pretty simple one:

  • Application A is running in a Kubernetes pod, and obtains lock for a long time (let's say 3 minutes)
  • Application B is trying to obtain the same lock but it doesn't know the secret so it's going to fail and retry later
  • Application A fails. Kubernetes automatically restarts the pod. The next instance of Application A will have to wait 2 and a half minutes (or if it knows the previous secret, it can reobtain the lock and it can continue or restart the process)
  • Application A finishes the process, releases the lock
  • Application B obtains the lock

janosmiko avatar Apr 03 '23 08:04 janosmiko

So to confirm, this is purely to prevent process/application A from waiting when trying to re-acquire the lock in step 3?

dim avatar Apr 03 '23 08:04 dim

The idea is to make it possible to release the lock from other processes. In this case, in step 3 the application is able to obtain (and release) the previous lock.

janosmiko avatar Apr 03 '23 09:04 janosmiko

@janosmiko Please see #66

dim avatar Apr 03 '23 16:04 dim

@dim looks solid to me :) any idea on timing for the release?

just judging whether i should roll my own temporarily for now, cheers

sambarnes avatar Apr 26 '23 17:04 sambarnes

@sambarnes sorry, forgot about this, will release today

dim avatar Apr 27 '23 08:04 dim

@dim legend 🙇

sambarnes avatar Apr 27 '23 13:04 sambarnes

Hey! @dim I think I'm missing something but I can not quite comprehend how this PR (https://github.com/bsm/redislock/pull/66) solves the issue. @janosmiko checks in his fork (https://github.com/janosmiko/redislock/blob/main/redislock.go see line 116) whether the stored value equals the value that should be stored. If this is the case it means the process trying to obtain the lock knows the shared secret/token (whatever you wanna call it;-)) and is allowed to reobtain it. Where is this done in your code?

michatw avatar Jul 18 '23 14:07 michatw

@michatw good point, missed this

dim avatar Jul 21 '23 11:07 dim

@michatw is this what you had in mind https://github.com/bsm/redislock/pull/71?

dim avatar Jul 21 '23 12:07 dim

@michatw is this what you had in mind #71?

@dim LGTM. Thanks for your fast response!

michatw avatar Jul 23 '23 08:07 michatw