feat: slow threshold customizable in redis (#1187)

master
Kevin Wan 3 years ago committed by GitHub
parent 8be0f77d96
commit f13e6f1149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +1,6 @@
package redis package redis
import ( import "errors"
"errors"
"time"
"github.com/tal-tech/go-zero/core/conf"
)
var ( var (
// ErrEmptyHost is an error that indicates no redis host is set. // ErrEmptyHost is an error that indicates no redis host is set.
@ -23,7 +18,6 @@ type (
Type string `json:",default=node,options=node|cluster"` Type string `json:",default=node,options=node|cluster"`
Pass string `json:",optional"` Pass string `json:",optional"`
Tls bool `json:",default=false,options=true|false"` Tls bool `json:",default=false,options=true|false"`
SlowThreshold time.Duration `json:",default=100ms"`
} }
// A RedisKeyConf is a redis config with key. // A RedisKeyConf is a redis config with key.
@ -42,9 +36,6 @@ func (rc RedisConf) NewRedis() *Redis {
if len(rc.Pass) > 0 { if len(rc.Pass) > 0 {
opts = append(opts, WithPass(rc.Pass)) opts = append(opts, WithPass(rc.Pass))
} }
if rc.SlowThreshold > 0 {
opts = append(opts, WithSlowThreshold(conf.CheckedDuration(rc.SlowThreshold)))
}
if rc.Tls { if rc.Tls {
opts = append(opts, WithTLS()) opts = append(opts, WithTLS())
} }

@ -2,7 +2,6 @@ package redis
import ( import (
"strings" "strings"
"time"
red "github.com/go-redis/redis" red "github.com/go-redis/redis"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
@ -10,14 +9,13 @@ import (
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
) )
func checkDuration(slowThreshold time.Duration) func(proc func(red.Cmder) error) func(red.Cmder) error { func checkDuration(proc func(red.Cmder) error) func(red.Cmder) error {
return func(proc func(red.Cmder) error) func(red.Cmder) error {
return func(cmd red.Cmder) error { return func(cmd red.Cmder) error {
start := timex.Now() start := timex.Now()
defer func() { defer func() {
duration := timex.Since(start) duration := timex.Since(start)
if duration > slowThreshold { if duration > slowThreshold.Load() {
var buf strings.Builder var buf strings.Builder
for i, arg := range cmd.Args() { for i, arg := range cmd.Args() {
if i > 0 { if i > 0 {
@ -32,4 +30,3 @@ func checkDuration(slowThreshold time.Duration) func(proc func(red.Cmder) error)
return proc(cmd) return proc(cmd)
} }
} }
}

@ -9,6 +9,7 @@ import (
red "github.com/go-redis/redis" red "github.com/go-redis/redis"
"github.com/tal-tech/go-zero/core/breaker" "github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/mapping" "github.com/tal-tech/go-zero/core/mapping"
"github.com/tal-tech/go-zero/core/syncx"
) )
const ( const (
@ -24,8 +25,11 @@ const (
defaultSlowThreshold = time.Millisecond * 100 defaultSlowThreshold = time.Millisecond * 100
) )
var (
// ErrNilNode is an error that indicates a nil redis node. // ErrNilNode is an error that indicates a nil redis node.
var ErrNilNode = errors.New("nil redis node") ErrNilNode = errors.New("nil redis node")
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
)
type ( type (
// Option defines the method to customize a Redis. // Option defines the method to customize a Redis.
@ -44,7 +48,6 @@ type (
Pass string Pass string
tls bool tls bool
brk breaker.Breaker brk breaker.Breaker
slowThreshold time.Duration
} }
// RedisNode interface represents a redis node. // RedisNode interface represents a redis node.
@ -81,7 +84,6 @@ func New(addr string, opts ...Option) *Redis {
Addr: addr, Addr: addr,
Type: NodeType, Type: NodeType,
brk: breaker.NewBreaker(), brk: breaker.NewBreaker(),
slowThreshold: defaultSlowThreshold,
} }
for _, opt := range opts { for _, opt := range opts {
@ -1759,6 +1761,11 @@ func Cluster() Option {
} }
} }
// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}
// WithPass customizes the given Redis with given password. // WithPass customizes the given Redis with given password.
func WithPass(pass string) Option { func WithPass(pass string) Option {
return func(r *Redis) { return func(r *Redis) {
@ -1766,13 +1773,6 @@ func WithPass(pass string) Option {
} }
} }
// WithSlowThreshold sets the slow threshold.
func WithSlowThreshold(threshold time.Duration) Option {
return func(r *Redis) {
r.slowThreshold = threshold
}
}
// WithTLS customizes the given Redis with TLS enabled. // WithTLS customizes the given Redis with TLS enabled.
func WithTLS() Option { func WithTLS() Option {
return func(r *Redis) { return func(r *Redis) {

@ -1073,6 +1073,12 @@ func TestRedisGeo(t *testing.T) {
}) })
} }
func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
SetSlowThreshold(time.Second)
assert.Equal(t, time.Second, slowThreshold.Load())
}
func TestRedis_WithPass(t *testing.T) { func TestRedis_WithPass(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
err := New(client.Addr, WithPass("any")).Ping() err := New(client.Addr, WithPass("any")).Ping()
@ -1115,7 +1121,7 @@ func runOnRedisTLS(t *testing.T, fn func(client *Redis)) {
client.Close() client.Close()
} }
}() }()
fn(New(s.Addr(), WithTLS(), WithSlowThreshold(defaultSlowThreshold/2))) fn(New(s.Addr(), WithTLS()))
} }
func badType() Option { func badType() Option {

@ -32,7 +32,7 @@ func getClient(r *Redis) (*red.Client, error) {
MinIdleConns: idleConns, MinIdleConns: idleConns,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
}) })
store.WrapProcess(checkDuration(r.slowThreshold)) store.WrapProcess(checkDuration)
return store, nil return store, nil
}) })
if err != nil { if err != nil {

@ -25,7 +25,7 @@ func getCluster(r *Redis) (*red.ClusterClient, error) {
MinIdleConns: idleConns, MinIdleConns: idleConns,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
}) })
store.WrapProcess(checkDuration(r.slowThreshold)) store.WrapProcess(checkDuration)
return store, nil return store, nil
}) })

Loading…
Cancel
Save