2019-04-08 05:05:15 -04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-09-27 17:09:46 -04:00
|
|
|
"context"
|
2019-04-08 05:05:15 -04:00
|
|
|
"crypto/tls"
|
2020-09-27 17:09:46 -04:00
|
|
|
"net"
|
2019-04-08 05:05:15 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Common options.
|
|
|
|
|
2020-09-27 17:09:46 -04:00
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
2019-04-08 05:05:15 -04:00
|
|
|
OnConnect func(*Conn) error
|
2020-09-27 17:09:46 -04:00
|
|
|
Username string
|
2019-04-08 05:05:15 -04:00
|
|
|
Password string
|
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
PoolSize int
|
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
|
|
|
|
// Only cluster clients.
|
|
|
|
|
|
|
|
MaxRedirects int
|
|
|
|
ReadOnly bool
|
|
|
|
RouteByLatency bool
|
|
|
|
RouteRandomly bool
|
|
|
|
|
|
|
|
// The sentinel master name.
|
|
|
|
// Only failover clients.
|
|
|
|
MasterName string
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:09:46 -04:00
|
|
|
// Cluster returns cluster options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Cluster() *ClusterOptions {
|
2019-04-08 05:05:15 -04:00
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClusterOptions{
|
|
|
|
Addrs: o.Addrs,
|
2020-09-27 17:09:46 -04:00
|
|
|
Dialer: o.Dialer,
|
2019-04-08 05:05:15 -04:00
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
2020-09-27 17:09:46 -04:00
|
|
|
Username: o.Username,
|
2019-04-08 05:05:15 -04:00
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRedirects: o.MaxRedirects,
|
|
|
|
ReadOnly: o.ReadOnly,
|
|
|
|
RouteByLatency: o.RouteByLatency,
|
|
|
|
RouteRandomly: o.RouteRandomly,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:09:46 -04:00
|
|
|
// Failover returns failover options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Failover() *FailoverOptions {
|
2019-04-08 05:05:15 -04:00
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:26379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FailoverOptions{
|
|
|
|
SentinelAddrs: o.Addrs,
|
|
|
|
MasterName: o.MasterName,
|
2020-09-27 17:09:46 -04:00
|
|
|
|
|
|
|
Dialer: o.Dialer,
|
|
|
|
OnConnect: o.OnConnect,
|
2019-04-08 05:05:15 -04:00
|
|
|
|
|
|
|
DB: o.DB,
|
2020-09-27 17:09:46 -04:00
|
|
|
Username: o.Username,
|
2019-04-08 05:05:15 -04:00
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:09:46 -04:00
|
|
|
// Simple returns basic options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Simple() *Options {
|
2019-04-08 05:05:15 -04:00
|
|
|
addr := "127.0.0.1:6379"
|
|
|
|
if len(o.Addrs) > 0 {
|
|
|
|
addr = o.Addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Options{
|
|
|
|
Addr: addr,
|
2020-09-27 17:09:46 -04:00
|
|
|
Dialer: o.Dialer,
|
2019-04-08 05:05:15 -04:00
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
|
|
|
DB: o.DB,
|
2020-09-27 17:09:46 -04:00
|
|
|
Username: o.Username,
|
2019-04-08 05:05:15 -04:00
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxConnAge: o.MaxConnAge,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
IdleTimeout: o.IdleTimeout,
|
|
|
|
IdleCheckFrequency: o.IdleCheckFrequency,
|
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
// UniversalClient is an abstract client which - based on the provided options -
|
2020-09-27 17:09:46 -04: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.
|
2019-04-08 05:05:15 -04:00
|
|
|
type UniversalClient interface {
|
|
|
|
Cmdable
|
2020-09-27 17:09:46 -04:00
|
|
|
Context() context.Context
|
|
|
|
AddHook(Hook)
|
2019-04-08 05:05:15 -04:00
|
|
|
Watch(fn func(*Tx) error, keys ...string) error
|
2020-09-27 17:09:46 -04:00
|
|
|
Do(args ...interface{}) *Cmd
|
|
|
|
DoContext(ctx context.Context, args ...interface{}) *Cmd
|
2019-04-08 05:05:15 -04:00
|
|
|
Process(cmd Cmder) error
|
2020-09-27 17:09:46 -04:00
|
|
|
ProcessContext(ctx context.Context, cmd Cmder) error
|
2019-04-08 05:05:15 -04:00
|
|
|
Subscribe(channels ...string) *PubSub
|
|
|
|
PSubscribe(channels ...string) *PubSub
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UniversalClient = (*Client)(nil)
|
|
|
|
var _ UniversalClient = (*ClusterClient)(nil)
|
2020-09-27 17:09:46 -04:00
|
|
|
var _ UniversalClient = (*Ring)(nil)
|
2019-04-08 05:05:15 -04: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 != "" {
|
2020-09-27 17:09:46 -04:00
|
|
|
return NewFailoverClient(opts.Failover())
|
2019-04-08 05:05:15 -04:00
|
|
|
} else if len(opts.Addrs) > 1 {
|
2020-09-27 17:09:46 -04:00
|
|
|
return NewClusterClient(opts.Cluster())
|
2019-04-08 05:05:15 -04:00
|
|
|
}
|
2020-09-27 17:09:46 -04:00
|
|
|
return NewClient(opts.Simple())
|
2019-04-08 05:05:15 -04:00
|
|
|
}
|