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