diff --git a/core/stores/redis/breakerhook.go b/core/stores/redis/breakerhook.go index c8bdce7da9fd..eeb398961c0b 100644 --- a/core/stores/redis/breakerhook.go +++ b/core/stores/redis/breakerhook.go @@ -5,13 +5,8 @@ import ( 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 } @@ -34,6 +29,12 @@ func (h breakerHook) ProcessHook(next red.ProcessHook) red.ProcessHook { func (h breakerHook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook { return func(ctx context.Context, cmds []red.Cmder) error { + for _, cmd := range cmds { + if _, ok := ignoreCmds[cmd.Name()]; ok { + return next(ctx, cmds) + } + } + return h.brk.DoWithAcceptableCtx(ctx, func() error { return next(ctx, cmds) }, acceptable) diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index ddc073ed029a..7c0963edfca6 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -10,6 +10,7 @@ import ( red "github.com/redis/go-redis/v9" "github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/errorx" + "github.com/zeromicro/go-zero/core/lang" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/mapping" "github.com/zeromicro/go-zero/core/syncx" @@ -33,6 +34,9 @@ var ( // ErrNilNode is an error that indicates a nil redis node. ErrNilNode = errors.New("nil redis node") slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) + ignoreCmds = map[string]lang.PlaceholderType{ + "blpop": {}, + } ) type ( @@ -2580,6 +2584,12 @@ func SetSlowThreshold(threshold time.Duration) { slowThreshold.Set(threshold) } +func SetIgnoreCmds(cmds ...string) { + for _, cmd := range cmds { + ignoreCmds[cmd] = lang.Placeholder + } +} + // WithHook customizes the given Redis with given durationHook. func WithHook(hook Hook) Option { return func(r *Redis) { diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index be0ad3d9aa28..f46c4f1eb152 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -2029,6 +2029,15 @@ func TestSetSlowThreshold(t *testing.T) { assert.Equal(t, time.Second, slowThreshold.Load()) } +func TestSetIgnoreCmds(t *testing.T) { + SetIgnoreCmds("hello") + _, ok := ignoreCmds["hello"] + assert.True(t, ok) + + _, ok = ignoreCmds["client"] + assert.False(t, ok) +} + func TestRedis_WithUserPass(t *testing.T) { runOnRedis(t, func(client *Redis) { err := newRedis(client.Addr, WithUser("any"), WithPass("any")).Ping()