forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/universal.go

385 lines
6.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"crypto/tls"
"net"
"time"
)
// UniversalOptions information is required by UniversalClient to establish
2024-02-18 10:42:21 +00:00
// connections.
2024-02-18 10:42:21 +00:00
type UniversalOptions struct {
2024-02-18 10:42:21 +00:00
// Either a single address or a seed list of host:port addresses
2024-02-18 10:42:21 +00:00
// of cluster/sentinel nodes.
2024-02-18 10:42:21 +00:00
Addrs []string
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
2024-02-18 10:42:21 +00:00
ClientName string
// Database to be selected after connecting to the server.
2024-02-18 10:42:21 +00:00
// Only single-node and failover clients.
2024-02-18 10:42:21 +00:00
DB int
// Common options.
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
2024-02-18 10:42:21 +00:00
OnConnect func(ctx context.Context, cn *Conn) error
Protocol int
Username string
Password string
2024-02-18 10:42:21 +00:00
SentinelUsername string
2024-02-18 10:42:21 +00:00
SentinelPassword string
MaxRetries int
2024-02-18 10:42:21 +00:00
MinRetryBackoff time.Duration
2024-02-18 10:42:21 +00:00
MaxRetryBackoff time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
2024-02-18 10:42:21 +00:00
ContextTimeoutEnabled bool
// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
2024-02-18 10:42:21 +00:00
PoolFIFO bool
PoolSize int
PoolTimeout time.Duration
MinIdleConns int
MaxIdleConns int
MaxActiveConns int
2024-02-18 10:42:21 +00:00
ConnMaxIdleTime time.Duration
2024-02-18 10:42:21 +00:00
ConnMaxLifetime time.Duration
TLSConfig *tls.Config
// Only cluster clients.
MaxRedirects int
ReadOnly bool
2024-02-18 10:42:21 +00:00
RouteByLatency bool
RouteRandomly bool
2024-02-18 10:42:21 +00:00
// The sentinel master name.
2024-02-18 10:42:21 +00:00
// Only failover clients.
MasterName string
DisableIndentity bool
IdentitySuffix string
2024-02-18 10:42:21 +00:00
}
// Cluster returns cluster options created from the universal options.
2024-02-18 10:42:21 +00:00
func (o *UniversalOptions) Cluster() *ClusterOptions {
2024-02-18 10:42:21 +00:00
if len(o.Addrs) == 0 {
2024-02-18 10:42:21 +00:00
o.Addrs = []string{"127.0.0.1:6379"}
2024-02-18 10:42:21 +00:00
}
return &ClusterOptions{
Addrs: o.Addrs,
2024-02-18 10:42:21 +00:00
ClientName: o.ClientName,
Dialer: o.Dialer,
OnConnect: o.OnConnect,
2024-02-18 10:42:21 +00:00
Protocol: o.Protocol,
2024-02-18 10:42:21 +00:00
Username: o.Username,
2024-02-18 10:42:21 +00:00
Password: o.Password,
MaxRedirects: o.MaxRedirects,
ReadOnly: o.ReadOnly,
2024-02-18 10:42:21 +00:00
RouteByLatency: o.RouteByLatency,
RouteRandomly: o.RouteRandomly,
MaxRetries: o.MaxRetries,
2024-02-18 10:42:21 +00:00
MinRetryBackoff: o.MinRetryBackoff,
2024-02-18 10:42:21 +00:00
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
2024-02-18 10:42:21 +00:00
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
PoolFIFO: o.PoolFIFO,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
MinIdleConns: o.MinIdleConns,
MaxIdleConns: o.MaxIdleConns,
MaxActiveConns: o.MaxActiveConns,
2024-02-18 10:42:21 +00:00
ConnMaxIdleTime: o.ConnMaxIdleTime,
2024-02-18 10:42:21 +00:00
ConnMaxLifetime: o.ConnMaxLifetime,
TLSConfig: o.TLSConfig,
DisableIndentity: o.DisableIndentity,
IdentitySuffix: o.IdentitySuffix,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Failover returns failover options created from the universal options.
2024-02-18 10:42:21 +00:00
func (o *UniversalOptions) Failover() *FailoverOptions {
2024-02-18 10:42:21 +00:00
if len(o.Addrs) == 0 {
2024-02-18 10:42:21 +00:00
o.Addrs = []string{"127.0.0.1:26379"}
2024-02-18 10:42:21 +00:00
}
return &FailoverOptions{
2024-02-18 10:42:21 +00:00
SentinelAddrs: o.Addrs,
MasterName: o.MasterName,
ClientName: o.ClientName,
Dialer: o.Dialer,
2024-02-18 10:42:21 +00:00
OnConnect: o.OnConnect,
DB: o.DB,
Protocol: o.Protocol,
Username: o.Username,
Password: o.Password,
2024-02-18 10:42:21 +00:00
SentinelUsername: o.SentinelUsername,
2024-02-18 10:42:21 +00:00
SentinelPassword: o.SentinelPassword,
MaxRetries: o.MaxRetries,
2024-02-18 10:42:21 +00:00
MinRetryBackoff: o.MinRetryBackoff,
2024-02-18 10:42:21 +00:00
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
2024-02-18 10:42:21 +00:00
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
PoolFIFO: o.PoolFIFO,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
MinIdleConns: o.MinIdleConns,
MaxIdleConns: o.MaxIdleConns,
MaxActiveConns: o.MaxActiveConns,
2024-02-18 10:42:21 +00:00
ConnMaxIdleTime: o.ConnMaxIdleTime,
2024-02-18 10:42:21 +00:00
ConnMaxLifetime: o.ConnMaxLifetime,
TLSConfig: o.TLSConfig,
DisableIndentity: o.DisableIndentity,
IdentitySuffix: o.IdentitySuffix,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Simple returns basic options created from the universal options.
2024-02-18 10:42:21 +00:00
func (o *UniversalOptions) Simple() *Options {
2024-02-18 10:42:21 +00:00
addr := "127.0.0.1:6379"
2024-02-18 10:42:21 +00:00
if len(o.Addrs) > 0 {
2024-02-18 10:42:21 +00:00
addr = o.Addrs[0]
2024-02-18 10:42:21 +00:00
}
return &Options{
Addr: addr,
2024-02-18 10:42:21 +00:00
ClientName: o.ClientName,
Dialer: o.Dialer,
OnConnect: o.OnConnect,
DB: o.DB,
2024-02-18 10:42:21 +00:00
Protocol: o.Protocol,
2024-02-18 10:42:21 +00:00
Username: o.Username,
2024-02-18 10:42:21 +00:00
Password: o.Password,
MaxRetries: o.MaxRetries,
2024-02-18 10:42:21 +00:00
MinRetryBackoff: o.MinRetryBackoff,
2024-02-18 10:42:21 +00:00
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
2024-02-18 10:42:21 +00:00
ContextTimeoutEnabled: o.ContextTimeoutEnabled,
PoolFIFO: o.PoolFIFO,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
MinIdleConns: o.MinIdleConns,
MaxIdleConns: o.MaxIdleConns,
MaxActiveConns: o.MaxActiveConns,
2024-02-18 10:42:21 +00:00
ConnMaxIdleTime: o.ConnMaxIdleTime,
2024-02-18 10:42:21 +00:00
ConnMaxLifetime: o.ConnMaxLifetime,
TLSConfig: o.TLSConfig,
DisableIndentity: o.DisableIndentity,
IdentitySuffix: o.IdentitySuffix,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// --------------------------------------------------------------------
// UniversalClient is an abstract client which - based on the provided options -
2024-02-18 10:42:21 +00:00
// represents either a ClusterClient, a FailoverClient, or a single-node Client.
2024-02-18 10:42:21 +00:00
// This can be useful for testing cluster-specific applications locally or having different
2024-02-18 10:42:21 +00:00
// clients in different environments.
2024-02-18 10:42:21 +00:00
type UniversalClient interface {
Cmdable
2024-02-18 10:42:21 +00:00
AddHook(Hook)
2024-02-18 10:42:21 +00:00
Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error
2024-02-18 10:42:21 +00:00
Do(ctx context.Context, args ...interface{}) *Cmd
2024-02-18 10:42:21 +00:00
Process(ctx context.Context, cmd Cmder) error
2024-02-18 10:42:21 +00:00
Subscribe(ctx context.Context, channels ...string) *PubSub
2024-02-18 10:42:21 +00:00
PSubscribe(ctx context.Context, channels ...string) *PubSub
2024-02-18 10:42:21 +00:00
SSubscribe(ctx context.Context, channels ...string) *PubSub
2024-02-18 10:42:21 +00:00
Close() error
2024-02-18 10:42:21 +00:00
PoolStats() *PoolStats
}
var (
_ UniversalClient = (*Client)(nil)
2024-02-18 10:42:21 +00:00
_ UniversalClient = (*ClusterClient)(nil)
2024-02-18 10:42:21 +00:00
_ UniversalClient = (*Ring)(nil)
)
// NewUniversalClient returns a new multi client. The type of the returned client depends
2024-02-18 10:42:21 +00:00
// on the following conditions:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
2024-02-18 10:42:21 +00:00
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
2024-02-18 10:42:21 +00:00
// 3. Otherwise, a single-node Client is returned.
2024-02-18 10:42:21 +00:00
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
2024-02-18 10:42:21 +00:00
if opts.MasterName != "" {
2024-02-18 10:42:21 +00:00
return NewFailoverClient(opts.Failover())
2024-02-18 10:42:21 +00:00
} else if len(opts.Addrs) > 1 {
2024-02-18 10:42:21 +00:00
return NewClusterClient(opts.Cluster())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return NewClient(opts.Simple())
2024-02-18 10:42:21 +00:00
}