From a464e4187a18731401fe77f04bf4ccf744ccd28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Fri, 26 Dec 2025 17:18:31 +0800 Subject: [PATCH] =?UTF-8?q?common/redis=E5=A2=9E=E5=8A=A0Lock=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis/redis.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/redis/redis.go b/redis/redis.go index b7297dd..09c60c2 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -33,11 +33,11 @@ func GetRedisClient() *gredis.Redis { } // Lock 分布式锁 -func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context)) { +func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) error { limit := 3 LOOP: if limit < 0 { - return + return nil } limit-- if val, err := RedisClient.Set(ctx, key, true, gredis.SetOption{ @@ -47,6 +47,7 @@ LOOP: NX: true, }); err != nil { glog.Errorf(ctx, "RedisClient.Lock error: %v", err) + return nil } else { if val.Bool() { defer func(RedisClient *gredis.Redis, ctx context.Context, key string) { @@ -54,7 +55,7 @@ LOOP: glog.Errorf(ctx, "RedisClient.Del error: %v", err) } }(RedisClient, ctx, key) - fn(ctx) + return fn(ctx) } else { time.Sleep(time.Second) goto LOOP