2019-04-08 14:35:15 +05:30
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-09-28 02:39:46 +05:30
|
|
|
"context"
|
2019-04-08 14:35:15 +05:30
|
|
|
"crypto/tls"
|
2020-09-28 02:39:46 +05:30
|
|
|
"net"
|
2019-04-08 14:35:15 +05:30
|
|
|
"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.
|
|
|
|
|
2021-02-11 02:58:32 +05:30
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
OnConnect func(ctx context.Context, cn *Conn) error
|
|
|
|
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
SentinelPassword string
|
|
|
|
|
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
|
|
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
|
2019-04-08 14:35:15 +05:30
|
|
|
PoolSize int
|
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
2021-02-11 02:58:32 +05:30
|
|
|
|
|
|
|
TLSConfig *tls.Config
|
2019-04-08 14:35:15 +05:30
|
|
|
|
|
|
|
// Only cluster clients.
|
|
|
|
|
|
|
|
MaxRedirects int
|
|
|
|
ReadOnly bool
|
|
|
|
RouteByLatency bool
|
|
|
|
RouteRandomly bool
|
|
|
|
|
|
|
|
// The sentinel master name.
|
|
|
|
// Only failover clients.
|
2021-04-23 05:38:53 +05:30
|
|
|
|
2019-04-08 14:35:15 +05:30
|
|
|
MasterName string
|
|
|
|
}
|
|
|
|
|
2020-09-28 02:39:46 +05:30
|
|
|
// Cluster returns cluster options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Cluster() *ClusterOptions {
|
2019-04-08 14:35:15 +05:30
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClusterOptions{
|
|
|
|
Addrs: o.Addrs,
|
2020-09-28 02:39:46 +05:30
|
|
|
Dialer: o.Dialer,
|
2019-04-08 14:35:15 +05:30
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
2020-09-28 02:39:46 +05:30
|
|
|
Username: o.Username,
|
2019-04-08 14:35:15 +05:30
|
|
|
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-28 02:39:46 +05:30
|
|
|
// Failover returns failover options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Failover() *FailoverOptions {
|
2019-04-08 14:35:15 +05:30
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:26379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FailoverOptions{
|
|
|
|
SentinelAddrs: o.Addrs,
|
|
|
|
MasterName: o.MasterName,
|
2020-09-28 02:39:46 +05:30
|
|
|
|
|
|
|
Dialer: o.Dialer,
|
|
|
|
OnConnect: o.OnConnect,
|
2019-04-08 14:35:15 +05:30
|
|
|
|
2021-02-11 02:58:32 +05:30
|
|
|
DB: o.DB,
|
|
|
|
Username: o.Username,
|
|
|
|
Password: o.Password,
|
|
|
|
SentinelPassword: o.SentinelPassword,
|
2019-04-08 14:35:15 +05:30
|
|
|
|
|
|
|
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-28 02:39:46 +05:30
|
|
|
// Simple returns basic options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Simple() *Options {
|
2019-04-08 14:35:15 +05:30
|
|
|
addr := "127.0.0.1:6379"
|
|
|
|
if len(o.Addrs) > 0 {
|
|
|
|
addr = o.Addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Options{
|
|
|
|
Addr: addr,
|
2020-09-28 02:39:46 +05:30
|
|
|
Dialer: o.Dialer,
|
2019-04-08 14:35:15 +05:30
|
|
|
OnConnect: o.OnConnect,
|
|
|
|
|
|
|
|
DB: o.DB,
|
2020-09-28 02:39:46 +05:30
|
|
|
Username: o.Username,
|
2019-04-08 14:35:15 +05:30
|
|
|
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 -
|
2021-04-23 05:38:53 +05:30
|
|
|
// represents either a ClusterClient, a FailoverClient, or a single-node Client.
|
|
|
|
// This can be useful for testing cluster-specific applications locally or having different
|
|
|
|
// clients in different environments.
|
2019-04-08 14:35:15 +05:30
|
|
|
type UniversalClient interface {
|
|
|
|
Cmdable
|
2020-09-28 02:39:46 +05:30
|
|
|
Context() context.Context
|
|
|
|
AddHook(Hook)
|
2021-02-11 02:58:32 +05:30
|
|
|
Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error
|
|
|
|
Do(ctx context.Context, args ...interface{}) *Cmd
|
|
|
|
Process(ctx context.Context, cmd Cmder) error
|
|
|
|
Subscribe(ctx context.Context, channels ...string) *PubSub
|
|
|
|
PSubscribe(ctx context.Context, channels ...string) *PubSub
|
2019-04-08 14:35:15 +05:30
|
|
|
Close() error
|
2021-02-11 02:58:32 +05:30
|
|
|
PoolStats() *PoolStats
|
2019-04-08 14:35:15 +05:30
|
|
|
}
|
|
|
|
|
2021-02-11 02:58:32 +05:30
|
|
|
var (
|
|
|
|
_ UniversalClient = (*Client)(nil)
|
|
|
|
_ UniversalClient = (*ClusterClient)(nil)
|
|
|
|
_ UniversalClient = (*Ring)(nil)
|
|
|
|
)
|
2019-04-08 14:35:15 +05:30
|
|
|
|
2021-04-23 05:38:53 +05:30
|
|
|
// NewUniversalClient returns a new multi client. The type of the returned client depends
|
|
|
|
// on the following conditions:
|
2019-04-08 14:35:15 +05:30
|
|
|
//
|
2021-04-23 05:38:53 +05:30
|
|
|
// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
|
|
|
|
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
|
|
|
|
// 3. Otherwise, a single-node Client is returned.
|
2019-04-08 14:35:15 +05:30
|
|
|
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
|
|
|
|
if opts.MasterName != "" {
|
2020-09-28 02:39:46 +05:30
|
|
|
return NewFailoverClient(opts.Failover())
|
2019-04-08 14:35:15 +05:30
|
|
|
} else if len(opts.Addrs) > 1 {
|
2020-09-28 02:39:46 +05:30
|
|
|
return NewClusterClient(opts.Cluster())
|
2019-04-08 14:35:15 +05:30
|
|
|
}
|
2020-09-28 02:39:46 +05:30
|
|
|
return NewClient(opts.Simple())
|
2019-04-08 14:35:15 +05:30
|
|
|
}
|