From 8745ed9c614a6811637eff1bd97759a9cf97de81 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 25 Dec 2021 19:44:27 +0800 Subject: [PATCH] chore: add 1s for tolerance in redislock (#1367) --- core/stores/redis/redislock.go | 7 ++----- core/stores/redis/redislock_test.go | 1 - 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/core/stores/redis/redislock.go b/core/stores/redis/redislock.go index 3e85d972..69077850 100644 --- a/core/stores/redis/redislock.go +++ b/core/stores/redis/redislock.go @@ -16,9 +16,7 @@ const ( else return 0 end` - randomLen = 16 - tolerance = 500 // milliseconds - millisPerSecond = 1000 + randomLen = 16 ) // A RedisLock is a redis lock. @@ -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 @@ -65,7 +63,6 @@ func (rl *RedisLock) Acquire() (bool, error) { } return true, nil - } // Release releases the lock. diff --git a/core/stores/redis/redislock_test.go b/core/stores/redis/redislock_test.go index d12ed03e..2234f403 100644 --- a/core/stores/redis/redislock_test.go +++ b/core/stores/redis/redislock_test.go @@ -49,6 +49,5 @@ func TestRedisLock(t *testing.T) { firstAcquire, err = firstLock.Acquire() assert.Nil(t, err) assert.True(t, firstAcquire) - }) }