Skip to content

Commit c578ab4

Browse files
committed
utils:redis锁恢复TryLockWithContext方法
1 parent 0166990 commit c578ab4

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

utils/redislock.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ type IRedisLocker interface {
2727
//
2828
// 该方法会立即返回锁定成功与否的结果
2929
TryLock(key string) bool
30+
// TryLockWithContext redis锁-尝试上锁
31+
//
32+
// using SetNX
33+
//
34+
// 与之对应的是使用 Unlock 或 UnlockWithContext 解锁
35+
//
36+
// # Note
37+
//
38+
// 该方法会立即返回锁定成功与否的结果
39+
TryLockWithContext(ctx context.Context, key string) bool
3040
// Lock redis锁-上锁
3141
//
3242
// using SetNX
@@ -110,6 +120,19 @@ func (rl redisLockerImpl) TryLock(key string) bool {
110120
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
111121
defer cancel()
112122

123+
return rl.TryLockWithContext(ctx, key)
124+
}
125+
126+
// TryLockWithContext redis锁-尝试上锁
127+
//
128+
// using SetNX
129+
//
130+
// 与之对应的是使用 Unlock 或 UnlockWithContext 解锁
131+
//
132+
// # Note
133+
//
134+
// 该方法会立即返回锁定成功与否的结果
135+
func (rl redisLockerImpl) TryLockWithContext(ctx context.Context, key string) bool {
113136
lockOk, _ := rl.client.SetNX(ctx, key, lockerValue(), lockTTL).Result()
114137

115138
//锁定成功,开始执行自动续期
@@ -187,7 +210,7 @@ func (rl redisLockerImpl) Lock(ctx context.Context, key string) {
187210
for {
188211
select {
189212
case <-ticker.C:
190-
if ok, redisErr := rl.client.ExpireXX(innerCtx, key, lockTTL).Result(); !ok || redisErr != nil {
213+
if expOk, expErr := rl.client.ExpireXX(innerCtx, key, lockTTL).Result(); !expOk || expErr != nil {
191214
break LOOP
192215
}
193216
case <-cancelChan:

utils/redislock_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ func TestRedisLockerImplLockerValue(t *testing.T) {
1717
}
1818

1919
func TestRedisLockPanic(t *testing.T) {
20+
t.Run("RedisLockPanic-Init-Panic", func(t *testing.T) {
21+
if redis.GetInstance() == nil && redis.GetClusterInstance() == nil {
22+
assert.Panics(t, func() {
23+
t.Log(RedisLocker())
24+
})
25+
}
26+
})
27+
2028
t.Run("RedisLockPanic-Lock", func(t *testing.T) {
2129
if redis.GetInstance() == nil && redis.GetClusterInstance() == nil {
2230
assert.Panics(t, func() {
@@ -31,7 +39,16 @@ func TestRedisLockPanic(t *testing.T) {
3139
t.Run("RedisLockPanic-TryLock", func(t *testing.T) {
3240
if redis.GetInstance() == nil && redis.GetClusterInstance() == nil {
3341
assert.Panics(t, func() {
34-
key := "go-sail-redisLocker-Lock"
42+
key := "go-sail-redisLocker-TryLock"
43+
RedisLocker().TryLock(key)
44+
})
45+
}
46+
})
47+
48+
t.Run("RedisLockPanic-TryLockWithContext", func(t *testing.T) {
49+
if redis.GetInstance() == nil && redis.GetClusterInstance() == nil {
50+
assert.Panics(t, func() {
51+
key := "go-sail-redisLocker-TryLockWithContext"
3552
RedisLocker().TryLock(key)
3653
})
3754
}
@@ -40,7 +57,7 @@ func TestRedisLockPanic(t *testing.T) {
4057
t.Run("RedisLockPanic-UnlockWithContext", func(t *testing.T) {
4158
if redis.GetInstance() == nil && redis.GetClusterInstance() == nil {
4259
assert.Panics(t, func() {
43-
key := "go-sail-redisLocker-Lock"
60+
key := "go-sail-redisLocker-UnlockWithContext"
4461
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
4562
defer cancel()
4663
RedisLocker().UnlockWithContext(ctx, key)
@@ -88,7 +105,7 @@ func TestRedisLock(t *testing.T) {
88105
})
89106

90107
t.Run("Unlock-WithContext", func(t *testing.T) {
91-
key := "go-sail-redisLocker-Unlock"
108+
key := "go-sail-redisLocker-UnlockWithContext"
92109
t.Log(RedisLocker().TryLock(key))
93110
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
94111
defer cancel()
@@ -148,7 +165,7 @@ func TestRedisClusterLock(t *testing.T) {
148165
})
149166

150167
t.Run("Unlock-WithContext", func(t *testing.T) {
151-
key := "go-sail-redisLocker-Unlock"
168+
key := "go-sail-redisLocker-UnlockWithContext"
152169
t.Log(RedisLocker().TryLock(key))
153170
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
154171
defer cancel()

0 commit comments

Comments
 (0)