Reputation: 641
I am using go-redis distributed lock
to realize mutual exclusion access, my server is a single thread server. But at the same time, many requests get the distributed lock
.
func (redisMgrPtr *RedisMgr) getLock(key string) (int32) {
encodeKey := transcoding.Base64Encode(key)
_, err := redisMgrPtr.redisClient.SetNX(redisMgrPtr.ctx,
encodeKey, 1, TIMEOUT).Result()
if err != nil {
return -1
}
return 0
}
func (redisMgrPtr *RedisMgr) delLock(key string, sessionId string) {
encodeKey := transcoding.Base64Encode(key)
redisMgrPtr.redisClient.Del(redisMgrPtr.ctx, encodeKey)
Log.Errorf("session[%s] del lock", sessionId)
}
Get lock code is like this:
func (redisMgrPtr *RedisMgr) GetServer(name string, session string) () {
for {
locRes := redisMgrPtr.getLock(name)
if locRes == 0 {
break
} else {
time.Sleep(5 * time.Millisecond)
continue
}
}
defer redisMgrPtr.delLock(sceneLock, sessionId)
Log.Errorf("session[%s] get lock", sessionId)
// do something
}
I find many requests get the lock at the same time, result is
2021-09-08T15:05:21.073+0800 session[51776955325] get lock
2021-09-08T15:05:21.073+0800 session[91776955325] get lock
2021-09-08T15:05:21.073+0800 session[71776955325] get lock
I think at the same time only one session can get the lock
Upvotes: 1
Views: 854
Reputation: 7440
The problem seems to be with the lock function. It does not check if the value existed or not, it just checks the error.
func (redisMgrPtr *RedisMgr) getLock(key string) (int32) {
encodeKey := transcoding.Base64Encode(key)
wasSet, err := redisMgrPtr.redisClient.SetNX(redisMgrPtr.ctx, encodeKey, 1, TIMEOUT).Result()
if err != nil || !wasSet {
return -1
}
return 0
}
... or using a boolean to make it easier to reason about:
func (redisMgrPtr *RedisMgr) getLock(key string) (ok bool) {
encodeKey := transcoding.Base64Encode(key)
wasSet, err := redisMgrPtr.redisClient.SetNX(redisMgrPtr.ctx, encodeKey, 1, TIMEOUT).Result()
return err == nil && wasSet
}
With the boolean version GetServer
could look like this:
func (redisMgrPtr *RedisMgr) GetServer(name string, session string) () {
for {
if ok := redisMgrPtr.getLock(name); ok {
break
}
time.Sleep(5 * time.Millisecond)
}
defer redisMgrPtr.delLock(sceneLock, sessionId)
Log.Errorf("session[%s] get lock", sessionId)
// do something
}
Upvotes: 2