2017-02-17 13:12:06 +03:00
|
|
|
package redis
|
|
|
|
|
2018-05-11 09:46:09 +03:00
|
|
|
import (
|
2019-05-31 16:36:57 +03:00
|
|
|
"context"
|
2018-05-11 09:46:09 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"time"
|
|
|
|
)
|
2017-02-17 13:12:06 +03:00
|
|
|
|
|
|
|
// UniversalOptions information is required by UniversalClient to establish
|
|
|
|
// connections.
|
|
|
|
type UniversalOptions struct {
|
|
|
|
// Either a single address or a seed list of host:port addresses
|
|
|
|
// of cluster/sentinel nodes.
|
|
|
|
Addrs []string
|
|
|
|
|
|
|
|
// Database to be selected after connecting to the server.
|
|
|
|
// Only single-node and failover clients.
|
|
|
|
DB int
|
|
|
|
|
2018-08-14 16:16:27 +03:00
|
|
|
// Common options.
|
2017-02-17 13:12:06 +03:00
|
|
|
|
2018-04-16 10:15:52 +03:00
|
|
|
OnConnect func(*Conn) error
|
2017-02-17 13:12:06 +03:00
|
|
|
Password string
|
2018-08-14 16:16:27 +03:00
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
2017-02-17 13:12:06 +03:00
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
PoolSize int
|
2018-08-14 16:16:27 +03:00
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
2017-02-17 13:12:06 +03:00
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
2018-05-11 09:46:09 +03:00
|
|
|
TLSConfig *tls.Config
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
// Only cluster clients.
|
|
|
|
|
|
|
|
MaxRedirects int
|
|
|
|
ReadOnly bool
|
|
|
|
RouteByLatency bool
|
|
|
|
RouteRandomly bool
|
|
|
|
|
|
|
|
// The sentinel master name.
|
|
|
|
// Only failover clients.
|
|
|
|
MasterName string
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *UniversalOptions) cluster() *ClusterOptions {
|
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClusterOptions{
|
2018-08-14 16:16:27 +03:00
|
|
|
Addrs: o.Addrs,
|
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
|
|
|
Password: o.Password,
|
|
|
|
|
2017-02-17 13:12:06 +03:00
|
|
|
MaxRedirects: o.MaxRedirects,
|
|
|
|
ReadOnly: o.ReadOnly,
|
2018-08-14 16:16:27 +03:00
|
|
|
RouteByLatency: o.RouteByLatency,
|
|
|
|
RouteRandomly: o.RouteRandomly,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
2017-02-17 13:12:06 +03:00
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
PoolSize: o.PoolSize,
|
2018-08-14 16:16:27 +03:00
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
2017-02-17 13:12:06 +03:00
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *UniversalOptions) failover() *FailoverOptions {
|
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:26379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FailoverOptions{
|
|
|
|
SentinelAddrs: o.Addrs,
|
|
|
|
MasterName: o.MasterName,
|
2018-08-14 16:16:27 +03:00
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
|
|
|
DB: o.DB,
|
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
2017-02-17 13:12:06 +03:00
|
|
|
|
|
|
|
PoolSize: o.PoolSize,
|
2018-08-14 16:16:27 +03:00
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
2017-02-17 13:12:06 +03:00
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *UniversalOptions) simple() *Options {
|
|
|
|
addr := "127.0.0.1:6379"
|
|
|
|
if len(o.Addrs) > 0 {
|
|
|
|
addr = o.Addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Options{
|
2018-08-14 16:16:27 +03:00
|
|
|
Addr: addr,
|
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
|
|
|
DB: o.DB,
|
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
2017-02-17 13:12:06 +03:00
|
|
|
|
|
|
|
PoolSize: o.PoolSize,
|
2018-08-14 16:16:27 +03:00
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
2017-02-17 13:12:06 +03:00
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
// UniversalClient is an abstract client which - based on the provided options -
|
2019-05-31 16:36:57 +03:00
|
|
|
// can connect to either clusters, or sentinel-backed failover instances
|
|
|
|
// or simple single-instance servers. This can be useful for testing
|
|
|
|
// cluster-specific applications locally.
|
2017-02-17 13:12:06 +03:00
|
|
|
type UniversalClient interface {
|
|
|
|
Cmdable
|
2019-05-31 16:36:57 +03:00
|
|
|
Context() context.Context
|
2019-05-31 17:40:47 +03:00
|
|
|
AddHook(Hook)
|
2018-05-07 20:28:11 +03:00
|
|
|
Watch(fn func(*Tx) error, keys ...string) error
|
2017-02-21 15:13:25 +03:00
|
|
|
Process(cmd Cmder) error
|
2017-12-15 16:10:18 +03:00
|
|
|
Subscribe(channels ...string) *PubSub
|
|
|
|
PSubscribe(channels ...string) *PubSub
|
2017-02-17 13:12:06 +03:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2017-09-25 11:48:44 +03:00
|
|
|
var _ UniversalClient = (*Client)(nil)
|
|
|
|
var _ UniversalClient = (*ClusterClient)(nil)
|
2019-05-31 17:40:47 +03:00
|
|
|
var _ UniversalClient = (*Ring)(nil)
|
2017-09-25 11:48:44 +03:00
|
|
|
|
2017-02-17 13:12:06 +03:00
|
|
|
// NewUniversalClient returns a new multi client. The type of client returned depends
|
|
|
|
// on the following three conditions:
|
|
|
|
//
|
|
|
|
// 1. if a MasterName is passed a sentinel-backed FailoverClient will be returned
|
|
|
|
// 2. if the number of Addrs is two or more, a ClusterClient will be returned
|
|
|
|
// 3. otherwise, a single-node redis Client will be returned.
|
|
|
|
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
|
|
|
|
if opts.MasterName != "" {
|
|
|
|
return NewFailoverClient(opts.failover())
|
|
|
|
} else if len(opts.Addrs) > 1 {
|
|
|
|
return NewClusterClient(opts.cluster())
|
|
|
|
}
|
|
|
|
return NewClient(opts.simple())
|
|
|
|
}
|