|
|
@ -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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ErrNilNode is an error that indicates a nil redis node.
|
|
|
|
var (
|
|
|
|
var ErrNilNode = errors.New("nil redis node")
|
|
|
|
// ErrNilNode is an error that indicates a 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.
|
|
|
@ -39,12 +43,11 @@ type (
|
|
|
|
|
|
|
|
|
|
|
|
// Redis defines a redis node/cluster. It is thread-safe.
|
|
|
|
// Redis defines a redis node/cluster. It is thread-safe.
|
|
|
|
Redis struct {
|
|
|
|
Redis struct {
|
|
|
|
Addr string
|
|
|
|
Addr string
|
|
|
|
Type string
|
|
|
|
Type string
|
|
|
|
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.
|
|
|
@ -78,10 +81,9 @@ type (
|
|
|
|
// New returns a Redis with given options.
|
|
|
|
// New returns a Redis with given options.
|
|
|
|
func New(addr string, opts ...Option) *Redis {
|
|
|
|
func New(addr string, opts ...Option) *Redis {
|
|
|
|
r := &Redis{
|
|
|
|
r := &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) {
|
|
|
|