feat: slow threshold customizable in redis (#1185)

* feat: slow threshold customizable in redis

* chore: improve config robustness
This commit is contained in:
Kevin Wan
2021-10-31 22:14:20 +08:00
committed by GitHub
parent b4d1c6da2c
commit 429f85a9de
8 changed files with 83 additions and 33 deletions

View File

@@ -21,8 +21,7 @@ const (
blockingQueryTimeout = 5 * time.Second
readWriteTimeout = 2 * time.Second
slowThreshold = time.Millisecond * 100
defaultSlowThreshold = time.Millisecond * 100
)
// ErrNilNode is an error that indicates a nil redis node.
@@ -40,11 +39,12 @@ type (
// Redis defines a redis node/cluster. It is thread-safe.
Redis struct {
Addr string
Type string
Pass string
tls bool
brk breaker.Breaker
Addr string
Type string
Pass string
tls bool
brk breaker.Breaker
slowThreshold time.Duration
}
// RedisNode interface represents a redis node.
@@ -78,9 +78,10 @@ type (
// New returns a Redis with given options.
func New(addr string, opts ...Option) *Redis {
r := &Redis{
Addr: addr,
Type: NodeType,
brk: breaker.NewBreaker(),
Addr: addr,
Type: NodeType,
brk: breaker.NewBreaker(),
slowThreshold: defaultSlowThreshold,
}
for _, opt := range opts {
@@ -1765,6 +1766,13 @@ 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) {