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

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

@ -9,6 +9,7 @@ import (
red "github.com/go-redis/redis"
"github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/mapping"
"github.com/tal-tech/go-zero/core/syncx"
)
const (
@ -24,8 +25,11 @@ const (
defaultSlowThreshold = time.Millisecond * 100
)
// ErrNilNode is an error that indicates a nil redis node.
var ErrNilNode = errors.New("nil redis node")
var (
// ErrNilNode is an error that indicates a nil redis node.
ErrNilNode = errors.New("nil redis node")
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
)
type (
// Option defines the method to customize a Redis.
@ -44,7 +48,6 @@ type (
Pass string
tls bool
brk breaker.Breaker
slowThreshold time.Duration
}
// RedisNode interface represents a redis node.
@ -81,7 +84,6 @@ func New(addr string, opts ...Option) *Redis {
Addr: addr,
Type: NodeType,
brk: breaker.NewBreaker(),
slowThreshold: defaultSlowThreshold,
}
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.
func WithPass(pass string) Option {
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.
func WithTLS() Option {
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) {
runOnRedis(t, func(client *Redis) {
err := New(client.Addr, WithPass("any")).Ping()
@ -1115,7 +1121,7 @@ func runOnRedisTLS(t *testing.T, fn func(client *Redis)) {
client.Close()
}
}()
fn(New(s.Addr(), WithTLS(), WithSlowThreshold(defaultSlowThreshold/2)))
fn(New(s.Addr(), WithTLS()))
}
func badType() Option {

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

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

Loading…
Cancel
Save