redis Lock方法 增加-锁重试次数耗尽返回信息

This commit is contained in:
2025-12-26 17:45:11 +08:00
committed by 张斌
parent 6c9e8b125a
commit c342345c0b

View File

@@ -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) {