Skip to content

Commit

Permalink
chore: add 1s for tolerance in redislock (zeromicro#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Dec 25, 2021
1 parent 836726e commit 8745ed9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
7 changes: 2 additions & 5 deletions core/stores/redis/redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const (
else
return 0
end`
randomLen = 16
tolerance = 500 // milliseconds
millisPerSecond = 1000
randomLen = 16
)

// A RedisLock is a redis lock.
Expand Down Expand Up @@ -51,7 +49,7 @@ func (rl *RedisLock) Acquire() (bool, error) {
}

seconds := atomic.LoadUint32(&rl.seconds)
ok, err := rl.store.SetnxEx(rl.key, rl.id, int(seconds))
ok, err := rl.store.SetnxEx(rl.key, rl.id, int(seconds+1)) // +1s for tolerance
if err == red.Nil {
atomic.AddInt32(&rl.count, -1)
return false, nil
Expand All @@ -65,7 +63,6 @@ func (rl *RedisLock) Acquire() (bool, error) {
}

return true, nil

}

// Release releases the lock.
Expand Down
1 change: 0 additions & 1 deletion core/stores/redis/redislock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ func TestRedisLock(t *testing.T) {
firstAcquire, err = firstLock.Acquire()
assert.Nil(t, err)
assert.True(t, firstAcquire)

})
}

0 comments on commit 8745ed9

Please sign in to comment.