feat: slow threshold customizable in redis (#1187)

This commit is contained in:
Kevin Wan
2021-11-01 08:20:35 +08:00
committed by GitHub
parent 8be0f77d96
commit f13e6f1149
6 changed files with 48 additions and 54 deletions

View File

@@ -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.
@@ -39,12 +43,11 @@ type (
// Redis defines a redis node/cluster. It is thread-safe.
Redis struct {
Addr string
Type string
Pass string
tls bool
brk breaker.Breaker
slowThreshold time.Duration
Addr string
Type string
Pass string
tls bool
brk breaker.Breaker
}
// RedisNode interface represents a redis node.
@@ -78,10 +81,9 @@ type (
// New returns a Redis with given options.
func New(addr string, opts ...Option) *Redis {
r := &Redis{
Addr: addr,
Type: NodeType,
brk: breaker.NewBreaker(),
slowThreshold: defaultSlowThreshold,
Addr: addr,
Type: NodeType,
brk: breaker.NewBreaker(),
}
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) {