bsm / redislock

Simplified distributed locking implementation using Redis
Other
1.45k stars 157 forks source link

How to get lock with a key #43

Closed tshubham19 closed 1 year ago

tshubham19 commented 2 years ago

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,

dim commented 2 years ago

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.

janosmiko commented 2 years ago

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

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

timo-klarshift commented 1 year ago

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

janosmiko commented 1 year ago

@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.

dim commented 1 year ago

@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.

janosmiko commented 1 year ago

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

dim commented 1 year ago

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

janosmiko commented 1 year ago

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.

dim commented 1 year ago

@janosmiko Please see #66

sambarnes commented 1 year ago

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

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

dim commented 1 year ago

@sambarnes sorry, forgot about this, will release today

sambarnes commented 1 year ago

@dim legend 🙇

michatw commented 1 year ago

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?

dim commented 1 year ago

@michatw good point, missed this

dim commented 1 year ago

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

michatw commented 1 year ago

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

@dim LGTM. Thanks for your fast response!