From 7d90f906f5a3e0a80db6e79716895b18a3b443b4 Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:21:33 +0800 Subject: [PATCH] feat: migrate redis breaker into hook (#3982) --- core/stores/redis/breakerhook.go | 41 + core/stores/redis/breakerhook_test.go | 135 + .../stores/redis/{hook.go => durationhook.go} | 13 +- .../{hook_test.go => durationhook_test.go} | 12 +- core/stores/redis/redis.go | 2356 +++++++---------- core/stores/redis/redis_test.go | 4 +- core/stores/redis/redisclientmanager.go | 7 +- core/stores/redis/redisclustermanager.go | 7 +- core/stores/redis/redisclustermanager_test.go | 2 +- 9 files changed, 1114 insertions(+), 1463 deletions(-) create mode 100644 core/stores/redis/breakerhook.go create mode 100644 core/stores/redis/breakerhook_test.go rename core/stores/redis/{hook.go => durationhook.go} (88%) rename core/stores/redis/{hook_test.go => durationhook_test.go} (87%) diff --git a/core/stores/redis/breakerhook.go b/core/stores/redis/breakerhook.go new file mode 100644 index 00000000..4d0a1bc0 --- /dev/null +++ b/core/stores/redis/breakerhook.go @@ -0,0 +1,41 @@ +package redis + +import ( + "context" + + red "github.com/redis/go-redis/v9" + "github.com/zeromicro/go-zero/core/breaker" + "github.com/zeromicro/go-zero/core/lang" +) + +var ignoreCmds = map[string]lang.PlaceholderType{ + "blpop": {}, +} + +type breakerHook struct { + brk breaker.Breaker +} + +func (h breakerHook) DialHook(next red.DialHook) red.DialHook { + return next +} + +func (h breakerHook) ProcessHook(next red.ProcessHook) red.ProcessHook { + return func(ctx context.Context, cmd red.Cmder) error { + if _, ok := ignoreCmds[cmd.Name()]; ok { + return next(ctx, cmd) + } + + return h.brk.DoWithAcceptable(func() error { + return next(ctx, cmd) + }, acceptable) + } +} + +func (h breakerHook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook { + return func(ctx context.Context, cmds []red.Cmder) error { + return h.brk.DoWithAcceptable(func() error { + return next(ctx, cmds) + }, acceptable) + } +} diff --git a/core/stores/redis/breakerhook_test.go b/core/stores/redis/breakerhook_test.go new file mode 100644 index 00000000..ebd41504 --- /dev/null +++ b/core/stores/redis/breakerhook_test.go @@ -0,0 +1,135 @@ +package redis + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/alicebob/miniredis/v2" + "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/breaker" +) + +func TestBreakerHook_ProcessHook(t *testing.T) { + t.Run("breakerHookOpen", func(t *testing.T) { + s := miniredis.RunT(t) + + rds := MustNewRedis(RedisConf{ + Host: s.Addr(), + Type: NodeType, + }) + + someError := errors.New("ERR some error") + s.SetError(someError.Error()) + + var err error + for i := 0; i < 1000; i++ { + _, err = rds.Get("key") + if err != nil && err.Error() != someError.Error() { + break + } + } + assert.Equal(t, breaker.ErrServiceUnavailable, err) + }) + + t.Run("breakerHookClose", func(t *testing.T) { + s := miniredis.RunT(t) + + rds := MustNewRedis(RedisConf{ + Host: s.Addr(), + Type: NodeType, + }) + + var err error + for i := 0; i < 1000; i++ { + _, err = rds.Get("key") + if err != nil { + break + } + } + assert.NotEqual(t, breaker.ErrServiceUnavailable, err) + }) + + t.Run("breakerHook_ignoreCmd", func(t *testing.T) { + s := miniredis.RunT(t) + + rds := MustNewRedis(RedisConf{ + Host: s.Addr(), + Type: NodeType, + }) + + someError := errors.New("ERR some error") + s.SetError(someError.Error()) + + var err error + + node, err := getRedis(rds) + assert.NoError(t, err) + + for i := 0; i < 1000; i++ { + _, err = rds.Blpop(node, "key") + if err != nil && err.Error() != someError.Error() { + break + } + } + assert.Equal(t, someError.Error(), err.Error()) + }) +} + +func TestBreakerHook_ProcessPipelineHook(t *testing.T) { + t.Run("breakerPipelineHookOpen", func(t *testing.T) { + s := miniredis.RunT(t) + + rds := MustNewRedis(RedisConf{ + Host: s.Addr(), + Type: NodeType, + }) + + someError := errors.New("ERR some error") + s.SetError(someError.Error()) + + var err error + for i := 0; i < 1000; i++ { + err = rds.Pipelined( + func(pipe Pipeliner) error { + pipe.Incr(context.Background(), "pipelined_counter") + pipe.Expire(context.Background(), "pipelined_counter", time.Hour) + pipe.ZAdd(context.Background(), "zadd", Z{Score: 12, Member: "zadd"}) + return nil + }, + ) + + if err != nil && err.Error() != someError.Error() { + break + } + } + assert.Equal(t, breaker.ErrServiceUnavailable, err) + }) + + t.Run("breakerPipelineHookClose", func(t *testing.T) { + s := miniredis.RunT(t) + + rds := MustNewRedis(RedisConf{ + Host: s.Addr(), + Type: NodeType, + }) + + var err error + for i := 0; i < 1000; i++ { + err = rds.Pipelined( + func(pipe Pipeliner) error { + pipe.Incr(context.Background(), "pipelined_counter") + pipe.Expire(context.Background(), "pipelined_counter", time.Hour) + pipe.ZAdd(context.Background(), "zadd", Z{Score: 12, Member: "zadd"}) + return nil + }, + ) + + if err != nil { + break + } + } + assert.NotEqual(t, breaker.ErrServiceUnavailable, err) + }) +} diff --git a/core/stores/redis/hook.go b/core/stores/redis/durationhook.go similarity index 88% rename from core/stores/redis/hook.go rename to core/stores/redis/durationhook.go index 5f09f94a..d453360a 100644 --- a/core/stores/redis/hook.go +++ b/core/stores/redis/durationhook.go @@ -23,17 +23,18 @@ import ( const spanName = "redis" var ( - durationHook = hook{} + defaultDurationHook = durationHook{} redisCmdsAttributeKey = attribute.Key("redis.cmds") ) -type hook struct{} +type durationHook struct { +} -func (h hook) DialHook(next red.DialHook) red.DialHook { +func (h durationHook) DialHook(next red.DialHook) red.DialHook { return next } -func (h hook) ProcessHook(next red.ProcessHook) red.ProcessHook { +func (h durationHook) ProcessHook(next red.ProcessHook) red.ProcessHook { return func(ctx context.Context, cmd red.Cmder) error { start := timex.Now() ctx, endSpan := h.startSpan(ctx, cmd) @@ -57,7 +58,7 @@ func (h hook) ProcessHook(next red.ProcessHook) red.ProcessHook { } } -func (h hook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook { +func (h durationHook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook { return func(ctx context.Context, cmds []red.Cmder) error { if len(cmds) == 0 { return next(ctx, cmds) @@ -83,7 +84,7 @@ func (h hook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipel } } -func (h hook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context, func(err error)) { +func (h durationHook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context, func(err error)) { tracer := trace.TracerFromContext(ctx) ctx, span := tracer.Start(ctx, diff --git a/core/stores/redis/hook_test.go b/core/stores/redis/durationhook_test.go similarity index 87% rename from core/stores/redis/hook_test.go rename to core/stores/redis/durationhook_test.go index 44b2e8fa..37c6eb54 100644 --- a/core/stores/redis/hook_test.go +++ b/core/stores/redis/durationhook_test.go @@ -21,7 +21,7 @@ func TestHookProcessCase1(t *testing.T) { tracetest.NewInMemoryExporter(t) w := logtest.NewCollector(t) - err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error { + err := defaultDurationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error { assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) return nil })(context.Background(), red.NewCmd(context.Background())) @@ -36,7 +36,7 @@ func TestHookProcessCase2(t *testing.T) { tracetest.NewInMemoryExporter(t) w := logtest.NewCollector(t) - err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error { + err := defaultDurationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error { assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) time.Sleep(slowThreshold.Load() + time.Millisecond) return nil @@ -54,12 +54,12 @@ func TestHookProcessPipelineCase1(t *testing.T) { tracetest.NewInMemoryExporter(t) w := logtest.NewCollector(t) - err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { + err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { return nil })(context.Background(), nil) assert.NoError(t, err) - err = durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { + err = defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) return nil })(context.Background(), []red.Cmder{ @@ -74,7 +74,7 @@ func TestHookProcessPipelineCase2(t *testing.T) { tracetest.NewInMemoryExporter(t) w := logtest.NewCollector(t) - err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { + err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) time.Sleep(slowThreshold.Load() + time.Millisecond) return nil @@ -91,7 +91,7 @@ func TestHookProcessPipelineCase2(t *testing.T) { func TestHookProcessPipelineCase3(t *testing.T) { te := tracetest.NewInMemoryExporter(t) - err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { + err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error { assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name()) return assert.AnError })(context.Background(), []red.Cmder{ diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index 8f2ee9f2..3c868534 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -156,21 +156,16 @@ func (s *Redis) BitCount(key string, start, end int64) (int64, error) { } // BitCountCtx is redis bitcount command implementation. -func (s *Redis) BitCountCtx(ctx context.Context, key string, start, end int64) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitCount(ctx, key, &red.BitCount{ - Start: start, - End: end, - }).Result() - return err - }, acceptable) +func (s *Redis) BitCountCtx(ctx context.Context, key string, start, end int64) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.BitCount(ctx, key, &red.BitCount{ + Start: start, + End: end, + }).Result() } // BitOpAnd is redis bit operation (and) command implementation. @@ -180,16 +175,12 @@ func (s *Redis) BitOpAnd(destKey string, keys ...string) (int64, error) { // BitOpAndCtx is redis bit operation (and) command implementation. func (s *Redis) BitOpAndCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitOpAnd(ctx, destKey, keys...).Result() - return err - }, acceptable) + conn, err := getRedis(s) + if err != nil { + return + } + val, err = conn.BitOpAnd(ctx, destKey, keys...).Result() return } @@ -200,16 +191,12 @@ func (s *Redis) BitOpNot(destKey, key string) (int64, error) { // BitOpNotCtx is redis bit operation (not) command implementation. func (s *Redis) BitOpNotCtx(ctx context.Context, destKey, key string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitOpNot(ctx, destKey, key).Result() - return err - }, acceptable) + conn, err := getRedis(s) + if err != nil { + return + } + val, err = conn.BitOpNot(ctx, destKey, key).Result() return } @@ -219,18 +206,13 @@ func (s *Redis) BitOpOr(destKey string, keys ...string) (int64, error) { } // BitOpOrCtx is redis bit operation (or) command implementation. -func (s *Redis) BitOpOrCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitOpOr(ctx, destKey, keys...).Result() - return err - }, acceptable) +func (s *Redis) BitOpOrCtx(ctx context.Context, destKey string, keys ...string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.BitOpOr(ctx, destKey, keys...).Result() } // BitOpXor is redis bit operation (xor) command implementation. @@ -239,18 +221,13 @@ func (s *Redis) BitOpXor(destKey string, keys ...string) (int64, error) { } // BitOpXorCtx is redis bit operation (xor) command implementation. -func (s *Redis) BitOpXorCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitOpXor(ctx, destKey, keys...).Result() - return err - }, acceptable) +func (s *Redis) BitOpXorCtx(ctx context.Context, destKey string, keys ...string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.BitOpXor(ctx, destKey, keys...).Result() } // BitPos is redis bitpos command implementation. @@ -259,18 +236,13 @@ func (s *Redis) BitPos(key string, bit, start, end int64) (int64, error) { } // BitPosCtx is redis bitpos command implementation. -func (s *Redis) BitPosCtx(ctx context.Context, key string, bit, start, end int64) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.BitPos(ctx, key, bit, start, end).Result() - return err - }, acceptable) +func (s *Redis) BitPosCtx(ctx context.Context, key string, bit, start, end int64) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.BitPos(ctx, key, bit, start, end).Result() } // Blpop uses passed in redis connection to execute blocking queries. @@ -342,18 +314,13 @@ func (s *Redis) Decr(key string) (int64, error) { } // DecrCtx is the implementation of redis decr command. -func (s *Redis) DecrCtx(ctx context.Context, key string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Decr(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) DecrCtx(ctx context.Context, key string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.Decr(ctx, key).Result() } // Decrby is the implementation of redis decrby command. @@ -362,18 +329,13 @@ func (s *Redis) Decrby(key string, decrement int64) (int64, error) { } // DecrbyCtx is the implementation of redis decrby command. -func (s *Redis) DecrbyCtx(ctx context.Context, key string, decrement int64) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.DecrBy(ctx, key, decrement).Result() - return err - }, acceptable) +func (s *Redis) DecrbyCtx(ctx context.Context, key string, decrement int64) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.DecrBy(ctx, key, decrement).Result() } // Del deletes keys. @@ -382,23 +344,18 @@ func (s *Redis) Del(keys ...string) (int, error) { } // DelCtx deletes keys. -func (s *Redis) DelCtx(ctx context.Context, keys ...string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.Del(ctx, keys...).Result() - if err != nil { - return err - } +func (s *Redis) DelCtx(ctx context.Context, keys ...string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.Del(ctx, keys...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Eval is the implementation of redis eval command. @@ -408,18 +365,13 @@ func (s *Redis) Eval(script string, keys []string, args ...any) (any, error) { // EvalCtx is the implementation of redis eval command. func (s *Redis) EvalCtx(ctx context.Context, script string, keys []string, - args ...any) (val any, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Eval(ctx, script, keys, args...).Result() - return err - }, acceptable) + args ...any) (any, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.Eval(ctx, script, keys, args...).Result() } // EvalSha is the implementation of redis evalsha command. @@ -429,18 +381,13 @@ func (s *Redis) EvalSha(sha string, keys []string, args ...any) (any, error) { // EvalShaCtx is the implementation of redis evalsha command. func (s *Redis) EvalShaCtx(ctx context.Context, sha string, keys []string, - args ...any) (val any, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.EvalSha(ctx, sha, keys, args...).Result() - return err - }, acceptable) + args ...any) (any, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.EvalSha(ctx, sha, keys, args...).Result() } // Exists is the implementation of redis exists command. @@ -449,23 +396,18 @@ func (s *Redis) Exists(key string) (bool, error) { } // ExistsCtx is the implementation of redis exists command. -func (s *Redis) ExistsCtx(ctx context.Context, key string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.Exists(ctx, key).Result() - if err != nil { - return err - } +func (s *Redis) ExistsCtx(ctx context.Context, key string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - val = v == 1 - return nil - }, acceptable) + v, err := conn.Exists(ctx, key).Result() + if err != nil { + return false, err + } - return + return v == 1, nil } // ExistsMany is the implementation of redis exists command. @@ -476,18 +418,13 @@ func (s *Redis) ExistsMany(keys ...string) (int64, error) { // ExistsManyCtx is the implementation of redis exists command. // checks the existence of multiple keys in Redis using the EXISTS command. -func (s *Redis) ExistsManyCtx(ctx context.Context, keys ...string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Exists(ctx, keys...).Result() - return err - }, acceptable) +func (s *Redis) ExistsManyCtx(ctx context.Context, keys ...string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.Exists(ctx, keys...).Result() } // Expire is the implementation of redis expire command. @@ -497,14 +434,12 @@ func (s *Redis) Expire(key string, seconds int) error { // ExpireCtx is the implementation of redis expire command. func (s *Redis) ExpireCtx(ctx context.Context, key string, seconds int) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.Expire(ctx, key, time.Duration(seconds)*time.Second).Err() - }, acceptable) + return conn.Expire(ctx, key, time.Duration(seconds)*time.Second).Err() } // Expireat is the implementation of redis expireat command. @@ -514,14 +449,12 @@ func (s *Redis) Expireat(key string, expireTime int64) error { // ExpireatCtx is the implementation of redis expireat command. func (s *Redis) ExpireatCtx(ctx context.Context, key string, expireTime int64) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.ExpireAt(ctx, key, time.Unix(expireTime, 0)).Err() - }, acceptable) + return conn.ExpireAt(ctx, key, time.Unix(expireTime, 0)).Err() } // GeoAdd is the implementation of redis geoadd command. @@ -530,19 +463,13 @@ func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (int64, error) { } // GeoAddCtx is the implementation of redis geoadd command. -func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) ( - val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoAdd(ctx, key, geoLocation...).Result() - return err - }, acceptable) +func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.GeoAdd(ctx, key, geoLocation...).Result() } // GeoDist is the implementation of redis geodist command. @@ -551,19 +478,13 @@ func (s *Redis) GeoDist(key, member1, member2, unit string) (float64, error) { } // GeoDistCtx is the implementation of redis geodist command. -func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) ( - val float64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoDist(ctx, key, member1, member2, unit).Result() - return err - }, acceptable) +func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) (float64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.GeoDist(ctx, key, member1, member2, unit).Result() } // GeoHash is the implementation of redis geohash command. @@ -573,18 +494,13 @@ func (s *Redis) GeoHash(key string, members ...string) ([]string, error) { // GeoHashCtx is the implementation of redis geohash command. func (s *Redis) GeoHashCtx(ctx context.Context, key string, members ...string) ( - val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoHash(ctx, key, members...).Result() - return err - }, acceptable) + []string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.GeoHash(ctx, key, members...).Result() } // GeoRadius is the implementation of redis georadius command. @@ -595,18 +511,13 @@ func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRad // GeoRadiusCtx is the implementation of redis georadius command. func (s *Redis) GeoRadiusCtx(ctx context.Context, key string, longitude, latitude float64, - query *GeoRadiusQuery) (val []GeoLocation, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoRadius(ctx, key, longitude, latitude, query).Result() - return err - }, acceptable) + query *GeoRadiusQuery) ([]GeoLocation, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.GeoRadius(ctx, key, longitude, latitude, query).Result() } // GeoRadiusByMember is the implementation of redis georadiusbymember command. @@ -616,18 +527,13 @@ func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) ([] // GeoRadiusByMemberCtx is the implementation of redis georadiusbymember command. func (s *Redis) GeoRadiusByMemberCtx(ctx context.Context, key, member string, - query *GeoRadiusQuery) (val []GeoLocation, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoRadiusByMember(ctx, key, member, query).Result() - return err - }, acceptable) + query *GeoRadiusQuery) ([]GeoLocation, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.GeoRadiusByMember(ctx, key, member, query).Result() } // GeoPos is the implementation of redis geopos command. @@ -637,18 +543,13 @@ func (s *Redis) GeoPos(key string, members ...string) ([]*GeoPos, error) { // GeoPosCtx is the implementation of redis geopos command. func (s *Redis) GeoPosCtx(ctx context.Context, key string, members ...string) ( - val []*GeoPos, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.GeoPos(ctx, key, members...).Result() - return err - }, acceptable) + []*GeoPos, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.GeoPos(ctx, key, members...).Result() } // Get is the implementation of redis get command. @@ -657,23 +558,19 @@ func (s *Redis) Get(key string) (string, error) { } // GetCtx is the implementation of redis get command. -func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - if val, err = conn.Get(ctx, key).Result(); errors.Is(err, red.Nil) { - return nil - } else if err != nil { - return err - } else { - return nil - } - }, acceptable) +func (s *Redis) GetCtx(ctx context.Context, key string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + if val, err := conn.Get(ctx, key).Result(); errors.Is(err, red.Nil) { + return "", nil + } else if err != nil { + return "", err + } else { + return val, nil + } } // GetBit is the implementation of redis getbit command. @@ -682,23 +579,18 @@ func (s *Redis) GetBit(key string, offset int64) (int, error) { } // GetBitCtx is the implementation of redis getbit command. -func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.GetBit(ctx, key, offset).Result() - if err != nil { - return err - } +func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.GetBit(ctx, key, offset).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // GetSet is the implementation of redis getset command. @@ -707,21 +599,18 @@ func (s *Redis) GetSet(key, value string) (string, error) { } // GetSetCtx is the implementation of redis getset command. -func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - if val, err = conn.GetSet(ctx, key, value).Result(); errors.Is(err, red.Nil) { - return nil - } +func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return err - }, acceptable) + val, err := conn.GetSet(ctx, key, value).Result() + if errors.Is(err, red.Nil) { + return "", nil + } - return + return val, err } // Hdel is the implementation of redis hdel command. @@ -730,23 +619,18 @@ func (s *Redis) Hdel(key string, fields ...string) (bool, error) { } // HdelCtx is the implementation of redis hdel command. -func (s *Redis) HdelCtx(ctx context.Context, key string, fields ...string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.HDel(ctx, key, fields...).Result() - if err != nil { - return err - } +func (s *Redis) HdelCtx(ctx context.Context, key string, fields ...string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - val = v >= 1 - return nil - }, acceptable) + v, err := conn.HDel(ctx, key, fields...).Result() + if err != nil { + return false, err + } - return + return v >= 1, nil } // Hexists is the implementation of redis hexists command. @@ -755,18 +639,13 @@ func (s *Redis) Hexists(key, field string) (bool, error) { } // HexistsCtx is the implementation of redis hexists command. -func (s *Redis) HexistsCtx(ctx context.Context, key, field string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HExists(ctx, key, field).Result() - return err - }, acceptable) +func (s *Redis) HexistsCtx(ctx context.Context, key, field string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.HExists(ctx, key, field).Result() } // Hget is the implementation of redis hget command. @@ -775,18 +654,13 @@ func (s *Redis) Hget(key, field string) (string, error) { } // HgetCtx is the implementation of redis hget command. -func (s *Redis) HgetCtx(ctx context.Context, key, field string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HGet(ctx, key, field).Result() - return err - }, acceptable) +func (s *Redis) HgetCtx(ctx context.Context, key, field string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.HGet(ctx, key, field).Result() } // Hgetall is the implementation of redis hgetall command. @@ -795,18 +669,13 @@ func (s *Redis) Hgetall(key string) (map[string]string, error) { } // HgetallCtx is the implementation of redis hgetall command. -func (s *Redis) HgetallCtx(ctx context.Context, key string) (val map[string]string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HGetAll(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) HgetallCtx(ctx context.Context, key string) (map[string]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.HGetAll(ctx, key).Result() } // Hincrby is the implementation of redis hincrby command. @@ -815,23 +684,18 @@ func (s *Redis) Hincrby(key, field string, increment int) (int, error) { } // HincrbyCtx is the implementation of redis hincrby command. -func (s *Redis) HincrbyCtx(ctx context.Context, key, field string, increment int) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.HIncrBy(ctx, key, field, int64(increment)).Result() - if err != nil { - return err - } +func (s *Redis) HincrbyCtx(ctx context.Context, key, field string, increment int) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.HIncrBy(ctx, key, field, int64(increment)).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // HincrbyFloat is the implementation of redis hincrbyfloat command. @@ -841,18 +705,13 @@ func (s *Redis) HincrbyFloat(key, field string, increment float64) (float64, err // HincrbyFloatCtx is the implementation of redis hincrbyfloat command. func (s *Redis) HincrbyFloatCtx(ctx context.Context, key, field string, increment float64) ( - val float64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HIncrByFloat(ctx, key, field, increment).Result() - return err - }, acceptable) + float64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.HIncrByFloat(ctx, key, field, increment).Result() } // Hkeys is the implementation of redis hkeys command. @@ -861,18 +720,13 @@ func (s *Redis) Hkeys(key string) ([]string, error) { } // HkeysCtx is the implementation of redis hkeys command. -func (s *Redis) HkeysCtx(ctx context.Context, key string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HKeys(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) HkeysCtx(ctx context.Context, key string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.HKeys(ctx, key).Result() } // Hlen is the implementation of redis hlen command. @@ -881,23 +735,18 @@ func (s *Redis) Hlen(key string) (int, error) { } // HlenCtx is the implementation of redis hlen command. -func (s *Redis) HlenCtx(ctx context.Context, key string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.HLen(ctx, key).Result() - if err != nil { - return err - } +func (s *Redis) HlenCtx(ctx context.Context, key string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.HLen(ctx, key).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Hmget is the implementation of redis hmget command. @@ -906,23 +755,18 @@ func (s *Redis) Hmget(key string, fields ...string) ([]string, error) { } // HmgetCtx is the implementation of redis hmget command. -func (s *Redis) HmgetCtx(ctx context.Context, key string, fields ...string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.HMGet(ctx, key, fields...).Result() - if err != nil { - return err - } +func (s *Redis) HmgetCtx(ctx context.Context, key string, fields ...string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toStrings(v) - return nil - }, acceptable) + v, err := conn.HMGet(ctx, key, fields...).Result() + if err != nil { + return nil, err + } - return + return toStrings(v), nil } // Hset is the implementation of redis hset command. @@ -932,14 +776,12 @@ func (s *Redis) Hset(key, field, value string) error { // HsetCtx is the implementation of redis hset command. func (s *Redis) HsetCtx(ctx context.Context, key, field, value string) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.HSet(ctx, key, field, value).Err() - }, acceptable) + return conn.HSet(ctx, key, field, value).Err() } // Hsetnx is the implementation of redis hsetnx command. @@ -948,18 +790,13 @@ func (s *Redis) Hsetnx(key, field, value string) (bool, error) { } // HsetnxCtx is the implementation of redis hsetnx command. -func (s *Redis) HsetnxCtx(ctx context.Context, key, field, value string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HSetNX(ctx, key, field, value).Result() - return err - }, acceptable) +func (s *Redis) HsetnxCtx(ctx context.Context, key, field, value string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.HSetNX(ctx, key, field, value).Result() } // Hmset is the implementation of redis hmset command. @@ -969,19 +806,17 @@ func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error { // HmsetCtx is the implementation of redis hmset command. func (s *Redis) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - vals := make(map[string]any, len(fieldsAndValues)) - for k, v := range fieldsAndValues { - vals[k] = v - } + vals := make(map[string]any, len(fieldsAndValues)) + for k, v := range fieldsAndValues { + vals[k] = v + } - return conn.HMSet(ctx, key, vals).Err() - }, acceptable) + return conn.HMSet(ctx, key, vals).Err() } // Hscan is the implementation of redis hscan command. @@ -992,18 +827,13 @@ func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) ( // HscanCtx is the implementation of redis hscan command. func (s *Redis) HscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) ( - keys []string, cur uint64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - keys, cur, err = conn.HScan(ctx, key, cursor, match, count).Result() - return err - }, acceptable) + []string, uint64, error) { + conn, err := getRedis(s) + if err != nil { + return nil, 0, err + } - return + return conn.HScan(ctx, key, cursor, match, count).Result() } // Hvals is the implementation of redis hvals command. @@ -1012,18 +842,13 @@ func (s *Redis) Hvals(key string) ([]string, error) { } // HvalsCtx is the implementation of redis hvals command. -func (s *Redis) HvalsCtx(ctx context.Context, key string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.HVals(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) HvalsCtx(ctx context.Context, key string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.HVals(ctx, key).Result() } // Incr is the implementation of redis incr command. @@ -1032,18 +857,13 @@ func (s *Redis) Incr(key string) (int64, error) { } // IncrCtx is the implementation of redis incr command. -func (s *Redis) IncrCtx(ctx context.Context, key string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Incr(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) IncrCtx(ctx context.Context, key string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.Incr(ctx, key).Result() } // Incrby is the implementation of redis incrby command. @@ -1052,18 +872,13 @@ func (s *Redis) Incrby(key string, increment int64) (int64, error) { } // IncrbyCtx is the implementation of redis incrby command. -func (s *Redis) IncrbyCtx(ctx context.Context, key string, increment int64) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.IncrBy(ctx, key, increment).Result() - return err - }, acceptable) +func (s *Redis) IncrbyCtx(ctx context.Context, key string, increment int64) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.IncrBy(ctx, key, increment).Result() } // IncrbyFloat is the implementation of redis hincrbyfloat command. @@ -1072,18 +887,13 @@ func (s *Redis) IncrbyFloat(key string, increment float64) (float64, error) { } // IncrbyFloatCtx is the implementation of redis hincrbyfloat command. -func (s *Redis) IncrbyFloatCtx(ctx context.Context, key string, increment float64) (val float64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.IncrByFloat(ctx, key, increment).Result() - return err - }, acceptable) +func (s *Redis) IncrbyFloatCtx(ctx context.Context, key string, increment float64) (float64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.IncrByFloat(ctx, key, increment).Result() } // Keys is the implementation of redis keys command. @@ -1092,18 +902,13 @@ func (s *Redis) Keys(pattern string) ([]string, error) { } // KeysCtx is the implementation of redis keys command. -func (s *Redis) KeysCtx(ctx context.Context, pattern string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Keys(ctx, pattern).Result() - return err - }, acceptable) +func (s *Redis) KeysCtx(ctx context.Context, pattern string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.Keys(ctx, pattern).Result() } // Llen is the implementation of redis llen command. @@ -1112,23 +917,18 @@ func (s *Redis) Llen(key string) (int, error) { } // LlenCtx is the implementation of redis llen command. -func (s *Redis) LlenCtx(ctx context.Context, key string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.LLen(ctx, key).Result() - if err != nil { - return err - } +func (s *Redis) LlenCtx(ctx context.Context, key string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.LLen(ctx, key).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Lindex is the implementation of redis lindex command. @@ -1137,18 +937,13 @@ func (s *Redis) Lindex(key string, index int64) (string, error) { } // LindexCtx is the implementation of redis lindex command. -func (s *Redis) LindexCtx(ctx context.Context, key string, index int64) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.LIndex(ctx, key, index).Result() - return err - }, acceptable) +func (s *Redis) LindexCtx(ctx context.Context, key string, index int64) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.LIndex(ctx, key, index).Result() } // Lpop is the implementation of redis lpop command. @@ -1157,18 +952,13 @@ func (s *Redis) Lpop(key string) (string, error) { } // LpopCtx is the implementation of redis lpop command. -func (s *Redis) LpopCtx(ctx context.Context, key string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.LPop(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) LpopCtx(ctx context.Context, key string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.LPop(ctx, key).Result() } // LpopCount is the implementation of redis lpopCount command. @@ -1177,18 +967,13 @@ func (s *Redis) LpopCount(key string, count int) ([]string, error) { } // LpopCountCtx is the implementation of redis lpopCount command. -func (s *Redis) LpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.LPopCount(ctx, key, count).Result() - return err - }, acceptable) +func (s *Redis) LpopCountCtx(ctx context.Context, key string, count int) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.LPopCount(ctx, key, count).Result() } // Lpush is the implementation of redis lpush command. @@ -1197,23 +982,18 @@ func (s *Redis) Lpush(key string, values ...any) (int, error) { } // LpushCtx is the implementation of redis lpush command. -func (s *Redis) LpushCtx(ctx context.Context, key string, values ...any) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.LPush(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) LpushCtx(ctx context.Context, key string, values ...any) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.LPush(ctx, key, values...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Lrange is the implementation of redis lrange command. @@ -1222,18 +1002,13 @@ func (s *Redis) Lrange(key string, start, stop int) ([]string, error) { } // LrangeCtx is the implementation of redis lrange command. -func (s *Redis) LrangeCtx(ctx context.Context, key string, start, stop int) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.LRange(ctx, key, int64(start), int64(stop)).Result() - return err - }, acceptable) +func (s *Redis) LrangeCtx(ctx context.Context, key string, start, stop int) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.LRange(ctx, key, int64(start), int64(stop)).Result() } // Lrem is the implementation of redis lrem command. @@ -1242,23 +1017,18 @@ func (s *Redis) Lrem(key string, count int, value string) (int, error) { } // LremCtx is the implementation of redis lrem command. -func (s *Redis) LremCtx(ctx context.Context, key string, count int, value string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.LRem(ctx, key, int64(count), value).Result() - if err != nil { - return err - } +func (s *Redis) LremCtx(ctx context.Context, key string, count int, value string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.LRem(ctx, key, int64(count), value).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Ltrim is the implementation of redis ltrim command. @@ -1268,14 +1038,12 @@ func (s *Redis) Ltrim(key string, start, stop int64) error { // LtrimCtx is the implementation of redis ltrim command. func (s *Redis) LtrimCtx(ctx context.Context, key string, start, stop int64) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.LTrim(ctx, key, start, stop).Err() - }, acceptable) + return conn.LTrim(ctx, key, start, stop).Err() } // Mget is the implementation of redis mget command. @@ -1284,23 +1052,18 @@ func (s *Redis) Mget(keys ...string) ([]string, error) { } // MgetCtx is the implementation of redis mget command. -func (s *Redis) MgetCtx(ctx context.Context, keys ...string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.MGet(ctx, keys...).Result() - if err != nil { - return err - } +func (s *Redis) MgetCtx(ctx context.Context, keys ...string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toStrings(v) - return nil - }, acceptable) + v, err := conn.MGet(ctx, keys...).Result() + if err != nil { + return nil, err + } - return + return toStrings(v), nil } // Mset is the implementation of redis mset command. @@ -1309,18 +1072,13 @@ func (s *Redis) Mset(fieldsAndValues ...any) (string, error) { } // MsetCtx is the implementation of redis mset command. -func (s *Redis) MsetCtx(ctx context.Context, fieldsAndValues ...any) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.MSet(ctx, fieldsAndValues...).Result() - return err - }, acceptable) +func (s *Redis) MsetCtx(ctx context.Context, fieldsAndValues ...any) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.MSet(ctx, fieldsAndValues...).Result() } // Persist is the implementation of redis persist command. @@ -1329,18 +1087,13 @@ func (s *Redis) Persist(key string) (bool, error) { } // PersistCtx is the implementation of redis persist command. -func (s *Redis) PersistCtx(ctx context.Context, key string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.Persist(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) PersistCtx(ctx context.Context, key string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.Persist(ctx, key).Result() } // Pfadd is the implementation of redis pfadd command. @@ -1349,23 +1102,18 @@ func (s *Redis) Pfadd(key string, values ...any) (bool, error) { } // PfaddCtx is the implementation of redis pfadd command. -func (s *Redis) PfaddCtx(ctx context.Context, key string, values ...any) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.PFAdd(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) PfaddCtx(ctx context.Context, key string, values ...any) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - val = v >= 1 - return nil - }, acceptable) + v, err := conn.PFAdd(ctx, key, values...).Result() + if err != nil { + return false, err + } - return + return v >= 1, nil } // Pfcount is the implementation of redis pfcount command. @@ -1374,18 +1122,13 @@ func (s *Redis) Pfcount(key string) (int64, error) { } // PfcountCtx is the implementation of redis pfcount command. -func (s *Redis) PfcountCtx(ctx context.Context, key string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.PFCount(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) PfcountCtx(ctx context.Context, key string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.PFCount(ctx, key).Result() } // Pfmerge is the implementation of redis pfmerge command. @@ -1395,15 +1138,13 @@ func (s *Redis) Pfmerge(dest string, keys ...string) error { // PfmergeCtx is the implementation of redis pfmerge command. func (s *Redis) PfmergeCtx(ctx context.Context, dest string, keys ...string) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - _, err = conn.PFMerge(ctx, dest, keys...).Result() + conn, err := getRedis(s) + if err != nil { return err - }, acceptable) + } + + _, err = conn.PFMerge(ctx, dest, keys...).Result() + return err } // Ping is the implementation of redis ping command. @@ -1412,26 +1153,19 @@ func (s *Redis) Ping() bool { } // PingCtx is the implementation of redis ping command. -func (s *Redis) PingCtx(ctx context.Context) (val bool) { +func (s *Redis) PingCtx(ctx context.Context) bool { // ignore error, error means false - _ = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - val = false - return nil - } - - v, err := conn.Ping(ctx).Result() - if err != nil { - val = false - return nil - } + conn, err := getRedis(s) + if err != nil { + return false + } - val = v == "PONG" - return nil - }, acceptable) + v, err := conn.Ping(ctx).Result() + if err != nil { + return false + } - return + return v == "PONG" } // Pipelined lets fn execute pipelined commands. @@ -1442,15 +1176,13 @@ func (s *Redis) Pipelined(fn func(Pipeliner) error) error { // PipelinedCtx lets fn execute pipelined commands. // Results need to be retrieved by calling Pipeline.Exec() func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - _, err = conn.Pipelined(ctx, fn) + conn, err := getRedis(s) + if err != nil { return err - }, acceptable) + } + + _, err = conn.Pipelined(ctx, fn) + return err } // Rpop is the implementation of redis rpop command. @@ -1459,18 +1191,13 @@ func (s *Redis) Rpop(key string) (string, error) { } // RpopCtx is the implementation of redis rpop command. -func (s *Redis) RpopCtx(ctx context.Context, key string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.RPop(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) RpopCtx(ctx context.Context, key string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.RPop(ctx, key).Result() } // RpopCount is the implementation of redis rpopCount command. @@ -1479,18 +1206,13 @@ func (s *Redis) RpopCount(key string, count int) ([]string, error) { } // RpopCountCtx is the implementation of redis rpopCount command. -func (s *Redis) RpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.RPopCount(ctx, key, count).Result() - return err - }, acceptable) +func (s *Redis) RpopCountCtx(ctx context.Context, key string, count int) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.RPopCount(ctx, key, count).Result() } // Rpush is the implementation of redis rpush command. @@ -1499,23 +1221,18 @@ func (s *Redis) Rpush(key string, values ...any) (int, error) { } // RpushCtx is the implementation of redis rpush command. -func (s *Redis) RpushCtx(ctx context.Context, key string, values ...any) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.RPush(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) RpushCtx(ctx context.Context, key string, values ...any) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.RPush(ctx, key, values...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Sadd is the implementation of redis sadd command. @@ -1524,23 +1241,18 @@ func (s *Redis) Sadd(key string, values ...any) (int, error) { } // SaddCtx is the implementation of redis sadd command. -func (s *Redis) SaddCtx(ctx context.Context, key string, values ...any) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SAdd(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) SaddCtx(ctx context.Context, key string, values ...any) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SAdd(ctx, key, values...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Scan is the implementation of redis scan command. @@ -1550,18 +1262,13 @@ func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, c // ScanCtx is the implementation of redis scan command. func (s *Redis) ScanCtx(ctx context.Context, cursor uint64, match string, count int64) ( - keys []string, cur uint64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - keys, cur, err = conn.Scan(ctx, cursor, match, count).Result() - return err - }, acceptable) + []string, uint64, error) { + conn, err := getRedis(s) + if err != nil { + return nil, 0, err + } - return + return conn.Scan(ctx, cursor, match, count).Result() } // SetBit is the implementation of redis setbit command. @@ -1570,23 +1277,18 @@ func (s *Redis) SetBit(key string, offset int64, value int) (int, error) { } // SetBitCtx is the implementation of redis setbit command. -func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value int) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SetBit(ctx, key, offset, value).Result() - if err != nil { - return err - } +func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value int) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SetBit(ctx, key, offset, value).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Sscan is the implementation of redis sscan command. @@ -1597,18 +1299,13 @@ func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) ( // SscanCtx is the implementation of redis sscan command. func (s *Redis) SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) ( - keys []string, cur uint64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - keys, cur, err = conn.SScan(ctx, key, cursor, match, count).Result() - return err - }, acceptable) + []string, uint64, error) { + conn, err := getRedis(s) + if err != nil { + return nil, 0, err + } - return + return conn.SScan(ctx, key, cursor, match, count).Result() } // Scard is the implementation of redis scard command. @@ -1617,19 +1314,14 @@ func (s *Redis) Scard(key string) (int64, error) { } // ScardCtx is the implementation of redis scard command. -func (s *Redis) ScardCtx(ctx context.Context, key string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } +func (s *Redis) ScardCtx(ctx context.Context, key string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val, err = conn.SCard(ctx, key).Result() - return err - }, acceptable) - - return -} + return conn.SCard(ctx, key).Result() +} // ScriptLoad is the implementation of redis script load command. func (s *Redis) ScriptLoad(script string) (string, error) { @@ -1652,17 +1344,13 @@ func (s *Redis) ScriptRun(script *Script, keys []string, args ...any) (any, erro } // ScriptRunCtx is the implementation of *redis.Script run command. -func (s *Redis) ScriptRunCtx(ctx context.Context, script *Script, keys []string, args ...any) (val any, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } +func (s *Redis) ScriptRunCtx(ctx context.Context, script *Script, keys []string, args ...any) (any, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val, err = script.Run(ctx, conn, keys, args...).Result() - return err - }, acceptable) - return + return script.Run(ctx, conn, keys, args...).Result() } // Set is the implementation of redis set command. @@ -1672,14 +1360,12 @@ func (s *Redis) Set(key, value string) error { // SetCtx is the implementation of redis set command. func (s *Redis) SetCtx(ctx context.Context, key, value string) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.Set(ctx, key, value, 0).Err() - }, acceptable) + return conn.Set(ctx, key, value, 0).Err() } // Setex is the implementation of redis setex command. @@ -1689,14 +1375,12 @@ func (s *Redis) Setex(key, value string, seconds int) error { // SetexCtx is the implementation of redis setex command. func (s *Redis) SetexCtx(ctx context.Context, key, value string, seconds int) error { - return s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return err + } - return conn.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err() - }, acceptable) + return conn.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err() } // Setnx is the implementation of redis setnx command. @@ -1705,18 +1389,13 @@ func (s *Redis) Setnx(key, value string) (bool, error) { } // SetnxCtx is the implementation of redis setnx command. -func (s *Redis) SetnxCtx(ctx context.Context, key, value string) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SetNX(ctx, key, value, 0).Result() - return err - }, acceptable) +func (s *Redis) SetnxCtx(ctx context.Context, key, value string) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.SetNX(ctx, key, value, 0).Result() } // SetnxEx is the implementation of redis setnx command with expire. @@ -1725,18 +1404,13 @@ func (s *Redis) SetnxEx(key, value string, seconds int) (bool, error) { } // SetnxExCtx is the implementation of redis setnx command with expire. -func (s *Redis) SetnxExCtx(ctx context.Context, key, value string, seconds int) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SetNX(ctx, key, value, time.Duration(seconds)*time.Second).Result() - return err - }, acceptable) +func (s *Redis) SetnxExCtx(ctx context.Context, key, value string, seconds int) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.SetNX(ctx, key, value, time.Duration(seconds)*time.Second).Result() } // Sismember is the implementation of redis sismember command. @@ -1745,18 +1419,13 @@ func (s *Redis) Sismember(key string, value any) (bool, error) { } // SismemberCtx is the implementation of redis sismember command. -func (s *Redis) SismemberCtx(ctx context.Context, key string, value any) (val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SIsMember(ctx, key, value).Result() - return err - }, acceptable) +func (s *Redis) SismemberCtx(ctx context.Context, key string, value any) (bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - return + return conn.SIsMember(ctx, key, value).Result() } // Smembers is the implementation of redis smembers command. @@ -1765,18 +1434,13 @@ func (s *Redis) Smembers(key string) ([]string, error) { } // SmembersCtx is the implementation of redis smembers command. -func (s *Redis) SmembersCtx(ctx context.Context, key string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SMembers(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) SmembersCtx(ctx context.Context, key string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.SMembers(ctx, key).Result() } // Spop is the implementation of redis spop command. @@ -1785,18 +1449,13 @@ func (s *Redis) Spop(key string) (string, error) { } // SpopCtx is the implementation of redis spop command. -func (s *Redis) SpopCtx(ctx context.Context, key string) (val string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SPop(ctx, key).Result() - return err - }, acceptable) +func (s *Redis) SpopCtx(ctx context.Context, key string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } - return + return conn.SPop(ctx, key).Result() } // Srandmember is the implementation of redis srandmember command. @@ -1805,18 +1464,13 @@ func (s *Redis) Srandmember(key string, count int) ([]string, error) { } // SrandmemberCtx is the implementation of redis srandmember command. -func (s *Redis) SrandmemberCtx(ctx context.Context, key string, count int) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SRandMemberN(ctx, key, int64(count)).Result() - return err - }, acceptable) +func (s *Redis) SrandmemberCtx(ctx context.Context, key string, count int) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.SRandMemberN(ctx, key, int64(count)).Result() } // Srem is the implementation of redis srem command. @@ -1825,23 +1479,18 @@ func (s *Redis) Srem(key string, values ...any) (int, error) { } // SremCtx is the implementation of redis srem command. -func (s *Redis) SremCtx(ctx context.Context, key string, values ...any) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SRem(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) SremCtx(ctx context.Context, key string, values ...any) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SRem(ctx, key, values...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // String returns the string representation of s. @@ -1855,18 +1504,13 @@ func (s *Redis) Sunion(keys ...string) ([]string, error) { } // SunionCtx is the implementation of redis sunion command. -func (s *Redis) SunionCtx(ctx context.Context, keys ...string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SUnion(ctx, keys...).Result() - return err - }, acceptable) +func (s *Redis) SunionCtx(ctx context.Context, keys ...string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.SUnion(ctx, keys...).Result() } // Sunionstore is the implementation of redis sunionstore command. @@ -1876,23 +1520,18 @@ func (s *Redis) Sunionstore(destination string, keys ...string) (int, error) { // SunionstoreCtx is the implementation of redis sunionstore command. func (s *Redis) SunionstoreCtx(ctx context.Context, destination string, keys ...string) ( - val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SUnionStore(ctx, destination, keys...).Result() - if err != nil { - return err - } + int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SUnionStore(ctx, destination, keys...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Sdiff is the implementation of redis sdiff command. @@ -1901,18 +1540,13 @@ func (s *Redis) Sdiff(keys ...string) ([]string, error) { } // SdiffCtx is the implementation of redis sdiff command. -func (s *Redis) SdiffCtx(ctx context.Context, keys ...string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SDiff(ctx, keys...).Result() - return err - }, acceptable) +func (s *Redis) SdiffCtx(ctx context.Context, keys ...string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.SDiff(ctx, keys...).Result() } // Sdiffstore is the implementation of redis sdiffstore command. @@ -1922,23 +1556,18 @@ func (s *Redis) Sdiffstore(destination string, keys ...string) (int, error) { // SdiffstoreCtx is the implementation of redis sdiffstore command. func (s *Redis) SdiffstoreCtx(ctx context.Context, destination string, keys ...string) ( - val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SDiffStore(ctx, destination, keys...).Result() - if err != nil { - return err - } + int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SDiffStore(ctx, destination, keys...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Sinter is the implementation of redis sinter command. @@ -1947,18 +1576,13 @@ func (s *Redis) Sinter(keys ...string) ([]string, error) { } // SinterCtx is the implementation of redis sinter command. -func (s *Redis) SinterCtx(ctx context.Context, keys ...string) (val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.SInter(ctx, keys...).Result() - return err - }, acceptable) +func (s *Redis) SinterCtx(ctx context.Context, keys ...string) ([]string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.SInter(ctx, keys...).Result() } // Sinterstore is the implementation of redis sinterstore command. @@ -1968,23 +1592,18 @@ func (s *Redis) Sinterstore(destination string, keys ...string) (int, error) { // SinterstoreCtx is the implementation of redis sinterstore command. func (s *Redis) SinterstoreCtx(ctx context.Context, destination string, keys ...string) ( - val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.SInterStore(ctx, destination, keys...).Result() - if err != nil { - return err - } + int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.SInterStore(ctx, destination, keys...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Ttl is the implementation of redis ttl command. @@ -1993,30 +1612,24 @@ func (s *Redis) Ttl(key string) (int, error) { } // TtlCtx is the implementation of redis ttl command. -func (s *Redis) TtlCtx(ctx context.Context, key string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - duration, err := conn.TTL(ctx, key).Result() - if err != nil { - return err - } +func (s *Redis) TtlCtx(ctx context.Context, key string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - if duration >= 0 { - val = int(duration / time.Second) - } else { - // -2 means key does not exist - // -1 means key exists but has no expire - val = int(duration) - } + duration, err := conn.TTL(ctx, key).Result() + if err != nil { + return 0, err + } - return nil - }, acceptable) + if duration >= 0 { + return int(duration / time.Second), nil + } - return + // -2 means key does not exist + // -1 means key exists but has no expire + return int(duration), nil } // Zadd is the implementation of redis zadd command. @@ -2037,26 +1650,21 @@ func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) // ZaddFloatCtx is the implementation of redis zadd command. func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) ( - val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZAdd(ctx, key, red.Z{ - Score: score, - Member: value, - }).Result() - if err != nil { - return err - } + bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - val = v == 1 - return nil - }, acceptable) + v, err := conn.ZAdd(ctx, key, red.Z{ + Score: score, + Member: value, + }).Result() + if err != nil { + return false, err + } - return + return v == 1, nil } // Zaddnx is the implementation of redis zadd nx command. @@ -2077,26 +1685,21 @@ func (s *Redis) ZaddnxFloat(key string, score float64, value string) (bool, erro // ZaddnxFloatCtx is the implementation of redis zaddnx command. func (s *Redis) ZaddnxFloatCtx(ctx context.Context, key string, score float64, value string) ( - val bool, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZAddNX(ctx, key, red.Z{ - Score: score, - Member: value, - }).Result() - if err != nil { - return err - } + bool, error) { + conn, err := getRedis(s) + if err != nil { + return false, err + } - val = v == 1 - return nil - }, acceptable) + v, err := conn.ZAddNX(ctx, key, red.Z{ + Score: score, + Member: value, + }).Result() + if err != nil { + return false, err + } - return + return v == 1, nil } // Zadds is the implementation of redis zadds command. @@ -2105,24 +1708,19 @@ func (s *Redis) Zadds(key string, ps ...Pair) (int64, error) { } // ZaddsCtx is the implementation of redis zadds command. -func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - var zs []red.Z - for _, p := range ps { - z := red.Z{Score: float64(p.Score), Member: p.Key} - zs = append(zs, z) - } +func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val, err = conn.ZAdd(ctx, key, zs...).Result() - return err - }, acceptable) + var zs []red.Z + for _, p := range ps { + z := red.Z{Score: float64(p.Score), Member: p.Key} + zs = append(zs, z) + } - return + return conn.ZAdd(ctx, key, zs...).Result() } // Zcard is the implementation of redis zcard command. @@ -2131,23 +1729,18 @@ func (s *Redis) Zcard(key string) (int, error) { } // ZcardCtx is the implementation of redis zcard command. -func (s *Redis) ZcardCtx(ctx context.Context, key string) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZCard(ctx, key).Result() - if err != nil { - return err - } +func (s *Redis) ZcardCtx(ctx context.Context, key string) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.ZCard(ctx, key).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Zcount is the implementation of redis zcount command. @@ -2156,24 +1749,19 @@ func (s *Redis) Zcount(key string, start, stop int64) (int, error) { } // ZcountCtx is the implementation of redis zcount command. -func (s *Redis) ZcountCtx(ctx context.Context, key string, start, stop int64) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZCount(ctx, key, strconv.FormatInt(start, 10), - strconv.FormatInt(stop, 10)).Result() - if err != nil { - return err - } +func (s *Redis) ZcountCtx(ctx context.Context, key string, start, stop int64) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.ZCount(ctx, key, strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10)).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Zincrby is the implementation of redis zincrby command. @@ -2183,23 +1771,18 @@ func (s *Redis) Zincrby(key string, increment int64, field string) (int64, error // ZincrbyCtx is the implementation of redis zincrby command. func (s *Redis) ZincrbyCtx(ctx context.Context, key string, increment int64, field string) ( - val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZIncrBy(ctx, key, float64(increment), field).Result() - if err != nil { - return err - } + int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int64(v) - return nil - }, acceptable) + v, err := conn.ZIncrBy(ctx, key, float64(increment), field).Result() + if err != nil { + return 0, err + } - return + return int64(v), nil } // Zscore is the implementation of redis zscore command. @@ -2208,23 +1791,18 @@ func (s *Redis) Zscore(key, value string) (int64, error) { } // ZscoreCtx is the implementation of redis zscore command. -func (s *Redis) ZscoreCtx(ctx context.Context, key, value string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZScore(ctx, key, value).Result() - if err != nil { - return err - } +func (s *Redis) ZscoreCtx(ctx context.Context, key, value string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int64(v) - return nil - }, acceptable) + v, err := conn.ZScore(ctx, key, value).Result() + if err != nil { + return 0, err + } - return + return int64(v), nil } // ZscoreByFloat is the implementation of redis zscore command score by float. @@ -2233,18 +1811,13 @@ func (s *Redis) ZscoreByFloat(key, value string) (float64, error) { } // ZscoreByFloatCtx is the implementation of redis zscore command score by float. -func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (val float64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZScore(ctx, key, value).Result() - return err - }, acceptable) +func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (float64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.ZScore(ctx, key, value).Result() } // Zscan is the implementation of redis zscan command. @@ -2255,18 +1828,13 @@ func (s *Redis) Zscan(key string, cursor uint64, match string, count int64) ( // ZscanCtx is the implementation of redis zscan command. func (s *Redis) ZscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) ( - keys []string, cur uint64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - keys, cur, err = conn.ZScan(ctx, key, cursor, match, count).Result() - return err - }, acceptable) + []string, uint64, error) { + conn, err := getRedis(s) + if err != nil { + return nil, 0, err + } - return + return conn.ZScan(ctx, key, cursor, match, count).Result() } // Zrank is the implementation of redis zrank command. @@ -2275,18 +1843,13 @@ func (s *Redis) Zrank(key, field string) (int64, error) { } // ZrankCtx is the implementation of redis zrank command. -func (s *Redis) ZrankCtx(ctx context.Context, key, field string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZRank(ctx, key, field).Result() - return err - }, acceptable) +func (s *Redis) ZrankCtx(ctx context.Context, key, field string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.ZRank(ctx, key, field).Result() } // Zrem is the implementation of redis zrem command. @@ -2295,23 +1858,18 @@ func (s *Redis) Zrem(key string, values ...any) (int, error) { } // ZremCtx is the implementation of redis zrem command. -func (s *Redis) ZremCtx(ctx context.Context, key string, values ...any) (val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRem(ctx, key, values...).Result() - if err != nil { - return err - } +func (s *Redis) ZremCtx(ctx context.Context, key string, values ...any) (int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.ZRem(ctx, key, values...).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Zremrangebyscore is the implementation of redis zremrangebyscore command. @@ -2321,24 +1879,19 @@ func (s *Redis) Zremrangebyscore(key string, start, stop int64) (int, error) { // ZremrangebyscoreCtx is the implementation of redis zremrangebyscore command. func (s *Redis) ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) ( - val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRemRangeByScore(ctx, key, strconv.FormatInt(start, 10), - strconv.FormatInt(stop, 10)).Result() - if err != nil { - return err - } + int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.ZRemRangeByScore(ctx, key, strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10)).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Zremrangebyrank is the implementation of redis zremrangebyrank command. @@ -2348,23 +1901,18 @@ func (s *Redis) Zremrangebyrank(key string, start, stop int64) (int, error) { // ZremrangebyrankCtx is the implementation of redis zremrangebyrank command. func (s *Redis) ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) ( - val int, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRemRangeByRank(ctx, key, start, stop).Result() - if err != nil { - return err - } + int, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - val = int(v) - return nil - }, acceptable) + v, err := conn.ZRemRangeByRank(ctx, key, start, stop).Result() + if err != nil { + return 0, err + } - return + return int(v), nil } // Zrange is the implementation of redis zrange command. @@ -2374,18 +1922,13 @@ func (s *Redis) Zrange(key string, start, stop int64) ([]string, error) { // ZrangeCtx is the implementation of redis zrange command. func (s *Redis) ZrangeCtx(ctx context.Context, key string, start, stop int64) ( - val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZRange(ctx, key, start, stop).Result() - return err - }, acceptable) + []string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.ZRange(ctx, key, start, stop).Result() } // ZrangeWithScores is the implementation of redis zrange command with scores. @@ -2395,23 +1938,18 @@ func (s *Redis) ZrangeWithScores(key string, start, stop int64) ([]Pair, error) // ZrangeWithScoresCtx is the implementation of redis zrange command with scores. func (s *Redis) ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ( - val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result() - if err != nil { - return err - } + []Pair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZrangeWithScoresByFloat is the implementation of redis zrange command with scores by float64. @@ -2421,23 +1959,18 @@ func (s *Redis) ZrangeWithScoresByFloat(key string, start, stop int64) ([]FloatP // ZrangeWithScoresByFloatCtx is the implementation of redis zrange command with scores by float64. func (s *Redis) ZrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) ( - val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result() - if err != nil { - return err - } + []FloatPair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // ZRevRangeWithScores is the implementation of redis zrevrange command with scores. @@ -2460,23 +1993,18 @@ func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, s // ZrevrangeWithScoresCtx is the implementation of redis zrevrange command with scores. func (s *Redis) ZrevrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ( - val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result() - if err != nil { - return err - } + []Pair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZRevRangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float. @@ -2499,23 +2027,18 @@ func (s *Redis) ZRevRangeWithScoresByFloatCtx(ctx context.Context, key string, s // ZrevrangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float. func (s *Redis) ZrevrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) ( - val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result() - if err != nil { - return err - } + []FloatPair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // ZrangebyscoreWithScores is the implementation of redis zrangebyscore command with scores. @@ -2525,26 +2048,21 @@ func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) ([]Pair, // ZrangebyscoreWithScoresCtx is the implementation of redis zrangebyscore command with scores. func (s *Redis) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ( - val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatInt(start, 10), - Max: strconv.FormatInt(stop, 10), - }).Result() - if err != nil { - return err - } + []Pair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatInt(start, 10), + Max: strconv.FormatInt(stop, 10), + }).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZrangebyscoreWithScoresByFloat is the implementation of redis zrangebyscore command with scores by float. @@ -2554,26 +2072,21 @@ func (s *Redis) ZrangebyscoreWithScoresByFloat(key string, start, stop float64) // ZrangebyscoreWithScoresByFloatCtx is the implementation of redis zrangebyscore command with scores by float. func (s *Redis) ZrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string, start, stop float64) ( - val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatFloat(start, 'f', -1, 64), - Max: strconv.FormatFloat(stop, 'f', -1, 64), - }).Result() - if err != nil { - return err - } + []FloatPair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatFloat(start, 'f', -1, 64), + Max: strconv.FormatFloat(stop, 'f', -1, 64), + }).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // ZrangebyscoreWithScoresAndLimit is the implementation of redis zrangebyscore command @@ -2586,32 +2099,27 @@ func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, // ZrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrangebyscore command // with scores and limit. func (s *Redis) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, - stop int64, page, size int) (val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - if size <= 0 { - return nil - } - - conn, err := getRedis(s) - if err != nil { - return err - } + stop int64, page, size int) ([]Pair, error) { + if size <= 0 { + return nil, nil + } - v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatInt(start, 10), - Max: strconv.FormatInt(stop, 10), - Offset: int64(page * size), - Count: int64(size), - }).Result() - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatInt(start, 10), + Max: strconv.FormatInt(stop, 10), + Offset: int64(page * size), + Count: int64(size), + }).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrangebyscore command @@ -2625,32 +2133,27 @@ func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimit(key string, start, stop f // ZrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrangebyscore command // with scores by float and limit. func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string, start, - stop float64, page, size int) (val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - if size <= 0 { - return nil - } - - conn, err := getRedis(s) - if err != nil { - return err - } + stop float64, page, size int) ([]FloatPair, error) { + if size <= 0 { + return nil, nil + } - v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatFloat(start, 'f', -1, 64), - Max: strconv.FormatFloat(stop, 'f', -1, 64), - Offset: int64(page * size), - Count: int64(size), - }).Result() - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatFloat(start, 'f', -1, 64), + Max: strconv.FormatFloat(stop, 'f', -1, 64), + Offset: int64(page * size), + Count: int64(size), + }).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // Zrevrange is the implementation of redis zrevrange command. @@ -2660,18 +2163,13 @@ func (s *Redis) Zrevrange(key string, start, stop int64) ([]string, error) { // ZrevrangeCtx is the implementation of redis zrevrange command. func (s *Redis) ZrevrangeCtx(ctx context.Context, key string, start, stop int64) ( - val []string, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZRevRange(ctx, key, start, stop).Result() - return err - }, acceptable) + []string, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - return + return conn.ZRevRange(ctx, key, start, stop).Result() } // ZrevrangebyscoreWithScores is the implementation of redis zrevrangebyscore command with scores. @@ -2681,26 +2179,21 @@ func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]Pai // ZrevrangebyscoreWithScoresCtx is the implementation of redis zrevrangebyscore command with scores. func (s *Redis) ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ( - val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatInt(start, 10), - Max: strconv.FormatInt(stop, 10), - }).Result() - if err != nil { - return err - } + []Pair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatInt(start, 10), + Max: strconv.FormatInt(stop, 10), + }).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZrevrangebyscoreWithScoresByFloat is the implementation of redis zrevrangebyscore command with scores by float. @@ -2711,26 +2204,21 @@ func (s *Redis) ZrevrangebyscoreWithScoresByFloat(key string, start, stop float6 // ZrevrangebyscoreWithScoresByFloatCtx is the implementation of redis zrevrangebyscore command with scores by float. func (s *Redis) ZrevrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string, - start, stop float64) (val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatFloat(start, 'f', -1, 64), - Max: strconv.FormatFloat(stop, 'f', -1, 64), - }).Result() - if err != nil { - return err - } + start, stop float64) ([]FloatPair, error) { + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatFloat(start, 'f', -1, 64), + Max: strconv.FormatFloat(stop, 'f', -1, 64), + }).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // ZrevrangebyscoreWithScoresAndLimit is the implementation of redis zrevrangebyscore command @@ -2744,32 +2232,27 @@ func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64 // ZrevrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrevrangebyscore command // with scores and limit. func (s *Redis) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, - start, stop int64, page, size int) (val []Pair, err error) { - err = s.brk.DoWithAcceptable(func() error { - if size <= 0 { - return nil - } - - conn, err := getRedis(s) - if err != nil { - return err - } + start, stop int64, page, size int) ([]Pair, error) { + if size <= 0 { + return nil, nil + } - v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatInt(start, 10), - Max: strconv.FormatInt(stop, 10), - Offset: int64(page * size), - Count: int64(size), - }).Result() - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatInt(start, 10), + Max: strconv.FormatInt(stop, 10), + Offset: int64(page * size), + Count: int64(size), + }).Result() + if err != nil { + return nil, err + } - return + return toPairs(v), nil } // ZrevrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrevrangebyscore command @@ -2783,32 +2266,27 @@ func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimit(key string, start, sto // ZrevrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrevrangebyscore command // with scores by float and limit. func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string, - start, stop float64, page, size int) (val []FloatPair, err error) { - err = s.brk.DoWithAcceptable(func() error { - if size <= 0 { - return nil - } - - conn, err := getRedis(s) - if err != nil { - return err - } + start, stop float64, page, size int) ([]FloatPair, error) { + if size <= 0 { + return nil, nil + } - v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ - Min: strconv.FormatFloat(start, 'f', -1, 64), - Max: strconv.FormatFloat(stop, 'f', -1, 64), - Offset: int64(page * size), - Count: int64(size), - }).Result() - if err != nil { - return err - } + conn, err := getRedis(s) + if err != nil { + return nil, err + } - val = toFloatPairs(v) - return nil - }, acceptable) + v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{ + Min: strconv.FormatFloat(start, 'f', -1, 64), + Max: strconv.FormatFloat(stop, 'f', -1, 64), + Offset: int64(page * size), + Count: int64(size), + }).Result() + if err != nil { + return nil, err + } - return + return toFloatPairs(v), nil } // Zrevrank is the implementation of redis zrevrank command. @@ -2817,18 +2295,13 @@ func (s *Redis) Zrevrank(key, field string) (int64, error) { } // ZrevrankCtx is the implementation of redis zrevrank command. -func (s *Redis) ZrevrankCtx(ctx context.Context, key, field string) (val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZRevRank(ctx, key, field).Result() - return err - }, acceptable) +func (s *Redis) ZrevrankCtx(ctx context.Context, key, field string) (int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.ZRevRank(ctx, key, field).Result() } // Zunionstore is the implementation of redis zunionstore command. @@ -2838,18 +2311,13 @@ func (s *Redis) Zunionstore(dest string, store *ZStore) (int64, error) { // ZunionstoreCtx is the implementation of redis zunionstore command. func (s *Redis) ZunionstoreCtx(ctx context.Context, dest string, store *ZStore) ( - val int64, err error) { - err = s.brk.DoWithAcceptable(func() error { - conn, err := getRedis(s) - if err != nil { - return err - } - - val, err = conn.ZUnionStore(ctx, dest, store).Result() - return err - }, acceptable) + int64, error) { + conn, err := getRedis(s) + if err != nil { + return 0, err + } - return + return conn.ZUnionStore(ctx, dest, store).Result() } func (s *Redis) checkConnection(pingTimeout time.Duration) error { @@ -2895,7 +2363,7 @@ func WithTLS() Option { } } -// withHook customizes the given Redis with given hook, only for private use now, +// withHook customizes the given Redis with given durationHook, only for private use now, // maybe expose later. func withHook(hook red.Hook) Option { return func(r *Redis) { diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index cdb082ab..4ddd2bfd 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -36,7 +36,7 @@ func (m myHook) ProcessHook(next red.ProcessHook) red.ProcessHook { if cmd.Name() == "ping" && !m.includePing { return next(ctx, cmd) } - return errors.New("hook error") + return errors.New("durationHook error") } } @@ -155,7 +155,7 @@ func TestRedis_NonBlock(t *testing.T) { t.Run("nonBlock true", func(t *testing.T) { s := miniredis.RunT(t) - // use hook to simulate redis ping error + // use durationHook to simulate redis ping error _, err := NewRedis(RedisConf{ Host: s.Addr(), NonBlock: true, diff --git a/core/stores/redis/redisclientmanager.go b/core/stores/redis/redisclientmanager.go index e2e1ad5f..14e60871 100644 --- a/core/stores/redis/redisclientmanager.go +++ b/core/stores/redis/redisclientmanager.go @@ -37,8 +37,11 @@ func getClient(r *Redis) (*red.Client, error) { MinIdleConns: idleConns, TLSConfig: tlsConfig, }) - store.AddHook(durationHook) - for _, hook := range r.hooks { + + hooks := append([]red.Hook{defaultDurationHook, breakerHook{ + brk: r.brk, + }}, r.hooks...) + for _, hook := range hooks { store.AddHook(hook) } diff --git a/core/stores/redis/redisclustermanager.go b/core/stores/redis/redisclustermanager.go index e361a577..5013de83 100644 --- a/core/stores/redis/redisclustermanager.go +++ b/core/stores/redis/redisclustermanager.go @@ -33,8 +33,11 @@ func getCluster(r *Redis) (*red.ClusterClient, error) { MinIdleConns: idleConns, TLSConfig: tlsConfig, }) - store.AddHook(durationHook) - for _, hook := range r.hooks { + + hooks := append([]red.Hook{defaultDurationHook, breakerHook{ + brk: r.brk, + }}, r.hooks...) + for _, hook := range hooks { store.AddHook(hook) } diff --git a/core/stores/redis/redisclustermanager_test.go b/core/stores/redis/redisclustermanager_test.go index ac31517e..ad418558 100644 --- a/core/stores/redis/redisclustermanager_test.go +++ b/core/stores/redis/redisclustermanager_test.go @@ -51,7 +51,7 @@ func TestGetCluster(t *testing.T) { Addr: r.Addr(), Type: ClusterType, tls: true, - hooks: []red.Hook{durationHook}, + hooks: []red.Hook{defaultDurationHook}, }) if assert.NoError(t, err) { assert.NotNil(t, c)