The "genValueFunc" only set m.value during "lock" but not in the "unlock"
Closed this issue · 5 comments
When trying to overwrite "genValueFunc" for each two thread share one same gen value. One of the behavior like:
m1 = newMutex(withGenValue)
m2 = newMutex(withGenValue)
m1.lock() -> true
m2.unlock() -> true
m2.lock() -> true
m1.unlock() -> true
But in the repo genValueFunc only call during the "lock" period, not on the "unlock" period. The unlock period m.value == "", which not initial.
Could we call GenValueFunc during the mutex initial phase, instead of the "lock" phase?
Else the genValueFunc behavior is not a common sense.
I am having the same issue. It appears it was not meant to be a lock one could re-acquire if the value is known. I think that is the source of the confusion. In my case I would like to leave the Mutex in a locked state after my request is complete. I would then take the value I have (passed in via the genValueFunc
Option) and use that to re-acquire / unlock the Mutex. It is fairly trivial to add, and I can send a PR for this but I want to make sure I am not introducing undesired API / behavior.
To be clear, my changes would allow this:
// Service A
someUUID := uuid.New()
mu := redsync.NewMutex("mutex-name", redsync.WithGenValueFunc(func() (string, error) {
return someUUID, nil
})
err := mu.Lock()
send someUUID to another service, Service B
// Service B
mu := redsync.NewMutex("mutex-name", redsync.WithGenValueFunc(func() (string, error) {
return someUUID, nil
})
err := mu.Unlock()
@BichengWang @schleppy I took quite a long time to get to this. Sorry about that.
I added a option for NewMutex (WithValue) and a method Value on Mutex. This is something that you can do now:
mutex1 := rs.NewMutex(key)
mutex1.Lock()
mutex2 := rs.NewMutex(key, WithValue(mutex1.Value()))
mutex2.Unlock()
This behavior should be mentioned in the docs, I couldn't figure out the problem with my code till I read this issue, should I add a PR for the docs?
That's great! Thanks for the above actions!
mutex3 := rs.NewMutex(key, WithValue("this is test value"))
mutex3.Lock()
So(mutex3.Value(), ShouldEqual, "this is test value")
-- false
Expected: 'this is test value'
Actual: 'ndPqnC9nNbxA8dZ1snfc+w=='
how can i get the value, witch i set?