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"
|
2019-05-18 14:00:07 +03:00
|
|
|
"net"
|
2018-05-11 09:46:09 +03:00
|
|
|
"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
|
|
|
|
|
2022-12-28 17:14:52 +03:00
|
|
|
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
|
|
|
|
ClientName string
|
|
|
|
|
2017-02-17 13:12:06 +03:00
|
|
|
// 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
|
|
|
|
2020-06-10 10:36:22 +03:00
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
OnConnect func(ctx context.Context, cn *Conn) error
|
|
|
|
|
2023-05-16 17:02:22 +03:00
|
|
|
Protocol int
|
2020-08-31 18:57:01 +03:00
|
|
|
Username string
|
|
|
|
Password string
|
2022-03-01 01:55:37 +03:00
|
|
|
SentinelUsername string
|
2020-08-31 18:57:01 +03:00
|
|
|
SentinelPassword string
|
2020-06-10 10:36:22 +03:00
|
|
|
|
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
|
|
|
|
2022-10-11 10:22:42 +03:00
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
ContextTimeoutEnabled bool
|
2020-06-10 10:36:22 +03:00
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
|
|
|
|
PoolFIFO bool
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolSize int
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
MinIdleConns int
|
|
|
|
MaxIdleConns int
|
2023-10-30 15:35:51 +03:00
|
|
|
MaxActiveConns int
|
2022-07-28 15:11:35 +03:00
|
|
|
ConnMaxIdleTime time.Duration
|
|
|
|
ConnMaxLifetime time.Duration
|
2020-06-10 10:36:22 +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.
|
2021-04-08 10:02:22 +03:00
|
|
|
|
2018-08-14 16:16:27 +03:00
|
|
|
MasterName string
|
2023-10-30 15:36:44 +03:00
|
|
|
|
|
|
|
DisableIndentity bool
|
2024-01-04 15:40:14 +03:00
|
|
|
IdentitySuffix string
|
2024-09-12 11:26:10 +03:00
|
|
|
UnstableResp3 bool
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:36:52 +03:00
|
|
|
// Cluster returns cluster options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Cluster() *ClusterOptions {
|
2017-02-17 13:12:06 +03:00
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClusterOptions{
|
2022-12-28 17:14:52 +03:00
|
|
|
Addrs: o.Addrs,
|
|
|
|
ClientName: o.ClientName,
|
|
|
|
Dialer: o.Dialer,
|
|
|
|
OnConnect: o.OnConnect,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
2023-05-16 17:02:22 +03:00
|
|
|
Protocol: o.Protocol,
|
2020-05-21 08:59:20 +03:00
|
|
|
Username: o.Username,
|
2018-08-14 16:16:27 +03:00
|
|
|
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
|
|
|
|
2022-10-11 10:22:42 +03:00
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
|
2022-07-28 15:11:35 +03:00
|
|
|
|
|
|
|
PoolFIFO: o.PoolFIFO,
|
|
|
|
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxIdleConns: o.MaxIdleConns,
|
2023-10-30 15:35:51 +03:00
|
|
|
MaxActiveConns: o.MaxActiveConns,
|
2022-07-28 15:11:35 +03:00
|
|
|
ConnMaxIdleTime: o.ConnMaxIdleTime,
|
|
|
|
ConnMaxLifetime: o.ConnMaxLifetime,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2023-10-30 15:36:44 +03:00
|
|
|
|
|
|
|
DisableIndentity: o.DisableIndentity,
|
2024-01-04 15:40:14 +03:00
|
|
|
IdentitySuffix: o.IdentitySuffix,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:36:52 +03:00
|
|
|
// Failover returns failover options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Failover() *FailoverOptions {
|
2017-02-17 13:12:06 +03:00
|
|
|
if len(o.Addrs) == 0 {
|
|
|
|
o.Addrs = []string{"127.0.0.1:26379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FailoverOptions{
|
|
|
|
SentinelAddrs: o.Addrs,
|
|
|
|
MasterName: o.MasterName,
|
2022-12-28 17:14:52 +03:00
|
|
|
ClientName: o.ClientName,
|
2019-05-18 14:00:07 +03:00
|
|
|
|
|
|
|
Dialer: o.Dialer,
|
|
|
|
OnConnect: o.OnConnect,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
2020-08-31 18:57:01 +03:00
|
|
|
DB: o.DB,
|
2023-05-16 17:02:22 +03:00
|
|
|
Protocol: o.Protocol,
|
2020-08-31 18:57:01 +03:00
|
|
|
Username: o.Username,
|
|
|
|
Password: o.Password,
|
2022-03-01 01:55:37 +03:00
|
|
|
SentinelUsername: o.SentinelUsername,
|
2020-08-31 18:57:01 +03:00
|
|
|
SentinelPassword: o.SentinelPassword,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
2022-10-11 10:22:42 +03:00
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
|
2017-02-17 13:12:06 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolFIFO: o.PoolFIFO,
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxIdleConns: o.MaxIdleConns,
|
2023-10-30 15:35:51 +03:00
|
|
|
MaxActiveConns: o.MaxActiveConns,
|
2022-07-28 15:11:35 +03:00
|
|
|
ConnMaxIdleTime: o.ConnMaxIdleTime,
|
|
|
|
ConnMaxLifetime: o.ConnMaxLifetime,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2023-10-30 15:36:44 +03:00
|
|
|
|
|
|
|
DisableIndentity: o.DisableIndentity,
|
2024-01-04 15:40:14 +03:00
|
|
|
IdentitySuffix: o.IdentitySuffix,
|
2024-09-12 11:26:10 +03:00
|
|
|
UnstableResp3: o.UnstableResp3,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:36:52 +03:00
|
|
|
// Simple returns basic options created from the universal options.
|
|
|
|
func (o *UniversalOptions) Simple() *Options {
|
2017-02-17 13:12:06 +03:00
|
|
|
addr := "127.0.0.1:6379"
|
|
|
|
if len(o.Addrs) > 0 {
|
|
|
|
addr = o.Addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Options{
|
2022-12-28 17:14:52 +03:00
|
|
|
Addr: addr,
|
|
|
|
ClientName: o.ClientName,
|
|
|
|
Dialer: o.Dialer,
|
|
|
|
OnConnect: o.OnConnect,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
DB: o.DB,
|
2023-05-16 17:02:22 +03:00
|
|
|
Protocol: o.Protocol,
|
2020-05-21 08:59:20 +03:00
|
|
|
Username: o.Username,
|
2018-08-14 16:16:27 +03:00
|
|
|
Password: o.Password,
|
|
|
|
|
|
|
|
MaxRetries: o.MaxRetries,
|
|
|
|
MinRetryBackoff: o.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: o.MaxRetryBackoff,
|
|
|
|
|
2022-10-11 10:22:42 +03:00
|
|
|
DialTimeout: o.DialTimeout,
|
|
|
|
ReadTimeout: o.ReadTimeout,
|
|
|
|
WriteTimeout: o.WriteTimeout,
|
|
|
|
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
|
2017-02-17 13:12:06 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolFIFO: o.PoolFIFO,
|
|
|
|
PoolSize: o.PoolSize,
|
|
|
|
PoolTimeout: o.PoolTimeout,
|
|
|
|
MinIdleConns: o.MinIdleConns,
|
|
|
|
MaxIdleConns: o.MaxIdleConns,
|
2023-10-30 15:35:51 +03:00
|
|
|
MaxActiveConns: o.MaxActiveConns,
|
2022-07-28 15:11:35 +03:00
|
|
|
ConnMaxIdleTime: o.ConnMaxIdleTime,
|
|
|
|
ConnMaxLifetime: o.ConnMaxLifetime,
|
2018-08-14 16:16:27 +03:00
|
|
|
|
|
|
|
TLSConfig: o.TLSConfig,
|
2023-10-30 15:36:44 +03:00
|
|
|
|
|
|
|
DisableIndentity: o.DisableIndentity,
|
2024-01-04 15:40:14 +03:00
|
|
|
IdentitySuffix: o.IdentitySuffix,
|
2024-09-12 11:26:10 +03:00
|
|
|
UnstableResp3: o.UnstableResp3,
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
// UniversalClient is an abstract client which - based on the provided options -
|
2021-04-08 10:02:22 +03:00
|
|
|
// 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.
|
2017-02-17 13:12:06 +03:00
|
|
|
type UniversalClient interface {
|
|
|
|
Cmdable
|
2019-05-31 17:40:47 +03:00
|
|
|
AddHook(Hook)
|
2020-03-11 17:26:42 +03:00
|
|
|
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
|
2022-08-03 18:10:03 +03:00
|
|
|
SSubscribe(ctx context.Context, channels ...string) *PubSub
|
2017-02-17 13:12:06 +03:00
|
|
|
Close() error
|
2020-09-29 19:08:01 +03:00
|
|
|
PoolStats() *PoolStats
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:52:07 +03:00
|
|
|
var (
|
|
|
|
_ UniversalClient = (*Client)(nil)
|
|
|
|
_ UniversalClient = (*ClusterClient)(nil)
|
|
|
|
_ UniversalClient = (*Ring)(nil)
|
|
|
|
)
|
2017-09-25 11:48:44 +03:00
|
|
|
|
2021-04-08 10:02:22 +03:00
|
|
|
// NewUniversalClient returns a new multi client. The type of the returned client depends
|
|
|
|
// on the following conditions:
|
2017-02-17 13:12:06 +03:00
|
|
|
//
|
2021-04-08 10:02:22 +03:00
|
|
|
// 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.
|
2017-02-17 13:12:06 +03:00
|
|
|
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
|
|
|
|
if opts.MasterName != "" {
|
2020-01-29 18:36:52 +03:00
|
|
|
return NewFailoverClient(opts.Failover())
|
2017-02-17 13:12:06 +03:00
|
|
|
} else if len(opts.Addrs) > 1 {
|
2020-01-29 18:36:52 +03:00
|
|
|
return NewClusterClient(opts.Cluster())
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|
2020-01-29 18:36:52 +03:00
|
|
|
return NewClient(opts.Simple())
|
2017-02-17 13:12:06 +03:00
|
|
|
}
|