redis Lock方法 增加-锁重试次数耗尽返回信息
This commit is contained in:
@@ -2,6 +2,7 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -37,7 +38,7 @@ func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx cont
|
||||
limit := 3
|
||||
LOOP:
|
||||
if limit < 0 {
|
||||
return
|
||||
return false, errors.New("锁重试次数耗尽")
|
||||
}
|
||||
limit--
|
||||
if val, err := RedisClient.Set(ctx, key, true, gredis.SetOption{
|
||||
@@ -46,7 +47,7 @@ LOOP:
|
||||
},
|
||||
NX: true,
|
||||
}); err != nil {
|
||||
return
|
||||
return false, err
|
||||
} else {
|
||||
if val.Bool() {
|
||||
defer func(RedisClient *gredis.Redis, ctx context.Context, key string) {
|
||||
|
||||
Reference in New Issue
Block a user