package redis

import (
	
	
	
	

	
	
	
)

// 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

	// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
	ClientName string

	// Database to be selected after connecting to the server.
	// Only single-node and failover clients.
	DB int

	// Common options.

	Dialer    func(ctx context.Context, network, addr string) (net.Conn, error)
	OnConnect func(ctx context.Context, cn *Conn) error

	Protocol int
	Username string
	Password string
	// CredentialsProvider allows the username and password to be updated
	// before reconnecting. It should return the current username and password.
	CredentialsProvider func() (username string, password string)

	// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
	// done to maintain API compatibility. In the future,
	// there might be a merge between CredentialsProviderContext and CredentialsProvider.
	// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
	CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)

	// StreamingCredentialsProvider is used to retrieve the credentials
	// for the connection from an external source. Those credentials may change
	// during the connection lifetime. This is useful for managed identity
	// scenarios where the credentials are retrieved from an external source.
	//
	// Currently, this is a placeholder for the future implementation.
	StreamingCredentialsProvider auth.StreamingCredentialsProvider

	SentinelUsername string
	SentinelPassword string

	MaxRetries      int
	MinRetryBackoff time.Duration
	MaxRetryBackoff time.Duration

	DialTimeout time.Duration

	// DialerRetries is the maximum number of retry attempts when dialing fails.
	//
	// default: 5
	DialerRetries int

	// DialerRetryTimeout is the backoff duration between retry attempts.
	//
	// default: 100 milliseconds
	DialerRetryTimeout time.Duration

	// ReadTimeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Supported values:
	//
	//	- `-1` - no timeout (block indefinitely).
	//	- `-2` - disables SetReadDeadline calls completely.
	//
	// default: 5 seconds
	ReadTimeout time.Duration

	// WriteTimeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking. Supported values:
	//
	//	- `-1` - no timeout (block indefinitely).
	//	- `-2` - disables SetWriteDeadline calls completely.
	//
	// default: 5 seconds (same as ReadTimeout, which it follows when unset)
	WriteTimeout time.Duration

	// ContextTimeoutEnabled controls whether the client respects context timeouts and deadlines.
	// See https://redis.io/docs/latest/develop/clients/go/produsage/#timeouts
	ContextTimeoutEnabled bool

	// ReadBufferSize is the size of the bufio.Reader buffer for each connection.
	// Larger buffers can improve performance for commands that return large responses.
	// Smaller buffers can improve memory usage for larger pools.
	//
	// default: 32KiB (32768 bytes)
	ReadBufferSize int

	// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
	// Larger buffers can improve performance for large pipelines and commands with many arguments.
	// Smaller buffers can improve memory usage for larger pools.
	//
	// default: 32KiB (32768 bytes)
	WriteBufferSize int

	// PipelineReadBufferSize / PipelineWriteBufferSize size the dedicated pipeline
	// pool's per-connection buffers. PipelinePoolSize sizes that pool; a negative
	// value opts out of the dedicated pipeline pool. See the same fields on
	// Options for details.
	PipelineReadBufferSize  int
	PipelineWriteBufferSize int
	PipelinePoolSize        int

	// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
	PoolFIFO bool

	PoolSize int

	// MaxConcurrentDials is the maximum number of concurrent connection creation goroutines.
	// If <= 0, defaults to PoolSize. If > PoolSize, it will be capped at PoolSize.
	MaxConcurrentDials int

	PoolTimeout           time.Duration
	MinIdleConns          int
	MaxIdleConns          int
	MaxActiveConns        int
	ConnMaxIdleTime       time.Duration
	ConnMaxLifetime       time.Duration
	ConnMaxLifetimeJitter time.Duration

	TLSConfig *tls.Config

	// Only cluster clients.

	MaxRedirects   int
	ReadOnly       bool
	RouteByLatency bool
	// RouteByLatencyTolerance is passed through to ClusterOptions and FailoverOptions;
	// see ClusterOptions.RouteByLatencyTolerance.
	RouteByLatencyTolerance time.Duration
	RouteRandomly           bool

	// MasterName is the sentinel master name.
	// Only for failover clients.
	MasterName string

	// DisableIndentity - Disable set-lib on connect.
	//
	// default: false
	//
	// Deprecated: Use DisableIdentity instead.
	DisableIndentity bool

	// DisableIdentity is used to disable CLIENT SETINFO command on connect.
	//
	// default: false
	DisableIdentity bool

	IdentitySuffix string

	// FailingTimeoutSeconds is the timeout in seconds for marking a cluster node as failing.
	// When a node is marked as failing, it will be avoided for this duration.
	// Only applies to cluster clients. Default is 15 seconds.
	FailingTimeoutSeconds int

	// Deprecated: All RediSearch commands now have stable RESP3 parsing and this
	// flag is a no-op. It is kept for backwards compatibility and will be removed
	// in a future release.
	UnstableResp3 bool

	// PushNotificationProcessor is the processor for handling push notifications.
	// If nil, a default processor will be created for RESP3 connections.
	PushNotificationProcessor push.NotificationProcessor

	// IsClusterMode can be used when only one Addrs is provided (e.g. Elasticache supports setting up cluster mode with configuration endpoint).
	IsClusterMode bool

	// AutoPipelineOptions is the default config for the client's
	// autopipeliner faces (AutoPipeline / AsyncAutoPipeline), applied when
	// they are called without explicit options. See Options.AutoPipelineOptions.
	AutoPipelineOptions *AutoPipelineOptions

	// MaintNotificationsConfig provides configuration for maintnotifications upgrades.
	MaintNotificationsConfig *maintnotifications.Config

	// ClientSideCacheConfig enables client-side caching when NewUniversalClient
	// selects a standalone Client. See Options.ClientSideCacheConfig.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCacheConfig *ClientSideCacheConfig

	// ClientSideCache supplies an explicit cache when NewUniversalClient selects
	// a standalone Client. See Options.ClientSideCache.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCache Cache

	// ClientSideCacheStrategy selects the standalone client's invalidation
	// strategy. See Options.ClientSideCacheStrategy.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCacheStrategy CSCStrategy

	// ClientSideCacheRefreshOnInvalidate re-fetches every cached entry of an
	// invalidated key as soon as its invalidation arrives, whether or not it was
	// read recently. See Options.ClientSideCacheRefreshOnInvalidate.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCacheRefreshOnInvalidate bool

	// ClientSideCacheCoalesceMisses coalesces concurrent cache misses onto a held
	// full-duplex connection. See Options.ClientSideCacheCoalesceMisses.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCacheCoalesceMisses bool

	// ClientSideCacheInvalidationBatchWindow batches invalidation-driven deletes.
	// See Options.ClientSideCacheInvalidationBatchWindow.
	//
	// Experimental: this API may change in a minor release.
	ClientSideCacheInvalidationBatchWindow time.Duration
}

// Cluster returns cluster options created from the universal options.
func ( *UniversalOptions) () *ClusterOptions {
	if len(.Addrs) == 0 {
		.Addrs = []string{"127.0.0.1:6379"}
	}

	return &ClusterOptions{
		Addrs:      .Addrs,
		ClientName: .ClientName,
		Dialer:     .Dialer,
		OnConnect:  .OnConnect,

		Protocol:                     .Protocol,
		Username:                     .Username,
		Password:                     .Password,
		CredentialsProvider:          .CredentialsProvider,
		CredentialsProviderContext:   .CredentialsProviderContext,
		StreamingCredentialsProvider: .StreamingCredentialsProvider,

		MaxRedirects:            .MaxRedirects,
		ReadOnly:                .ReadOnly,
		RouteByLatency:          .RouteByLatency,
		RouteByLatencyTolerance: .RouteByLatencyTolerance,
		RouteRandomly:           .RouteRandomly,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:        .DialTimeout,
		DialerRetries:      .DialerRetries,
		DialerRetryTimeout: .DialerRetryTimeout,
		ReadTimeout:        .ReadTimeout,
		WriteTimeout:       .WriteTimeout,

		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		ReadBufferSize:  .ReadBufferSize,
		WriteBufferSize: .WriteBufferSize,

		PipelineReadBufferSize:  .PipelineReadBufferSize,
		PipelineWriteBufferSize: .PipelineWriteBufferSize,
		PipelinePoolSize:        .PipelinePoolSize,

		PoolFIFO:              .PoolFIFO,
		PoolSize:              .PoolSize,
		MaxConcurrentDials:    .MaxConcurrentDials,
		PoolTimeout:           .PoolTimeout,
		MinIdleConns:          .MinIdleConns,
		MaxIdleConns:          .MaxIdleConns,
		MaxActiveConns:        .MaxActiveConns,
		ConnMaxIdleTime:       .ConnMaxIdleTime,
		ConnMaxLifetime:       .ConnMaxLifetime,
		ConnMaxLifetimeJitter: .ConnMaxLifetimeJitter,

		TLSConfig: .TLSConfig,

		DisableIdentity:           .DisableIdentity,
		DisableIndentity:          .DisableIndentity,
		IdentitySuffix:            .IdentitySuffix,
		AutoPipelineOptions:       .AutoPipelineOptions,
		FailingTimeoutSeconds:     .FailingTimeoutSeconds,
		UnstableResp3:             .UnstableResp3,
		PushNotificationProcessor: .PushNotificationProcessor,
		MaintNotificationsConfig:  .MaintNotificationsConfig,
	}
}

// Failover returns failover options created from the universal options.
func ( *UniversalOptions) () *FailoverOptions {
	if len(.Addrs) == 0 {
		.Addrs = []string{"127.0.0.1:26379"}
	}

	return &FailoverOptions{
		SentinelAddrs: .Addrs,
		MasterName:    .MasterName,
		ClientName:    .ClientName,

		Dialer:    .Dialer,
		OnConnect: .OnConnect,

		DB:                           .DB,
		Protocol:                     .Protocol,
		Username:                     .Username,
		Password:                     .Password,
		CredentialsProvider:          .CredentialsProvider,
		CredentialsProviderContext:   .CredentialsProviderContext,
		StreamingCredentialsProvider: .StreamingCredentialsProvider,

		SentinelUsername: .SentinelUsername,
		SentinelPassword: .SentinelPassword,

		RouteByLatency:          .RouteByLatency,
		RouteByLatencyTolerance: .RouteByLatencyTolerance,
		RouteRandomly:           .RouteRandomly,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:        .DialTimeout,
		DialerRetries:      .DialerRetries,
		DialerRetryTimeout: .DialerRetryTimeout,
		ReadTimeout:        .ReadTimeout,
		WriteTimeout:       .WriteTimeout,

		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		ReadBufferSize:  .ReadBufferSize,
		WriteBufferSize: .WriteBufferSize,

		PipelineReadBufferSize:  .PipelineReadBufferSize,
		PipelineWriteBufferSize: .PipelineWriteBufferSize,
		PipelinePoolSize:        .PipelinePoolSize,

		PoolFIFO:              .PoolFIFO,
		PoolSize:              .PoolSize,
		MaxConcurrentDials:    .MaxConcurrentDials,
		PoolTimeout:           .PoolTimeout,
		MinIdleConns:          .MinIdleConns,
		MaxIdleConns:          .MaxIdleConns,
		MaxActiveConns:        .MaxActiveConns,
		ConnMaxIdleTime:       .ConnMaxIdleTime,
		ConnMaxLifetime:       .ConnMaxLifetime,
		ConnMaxLifetimeJitter: .ConnMaxLifetimeJitter,

		TLSConfig: .TLSConfig,

		ReplicaOnly: .ReadOnly,

		DisableIdentity:           .DisableIdentity,
		DisableIndentity:          .DisableIndentity,
		IdentitySuffix:            .IdentitySuffix,
		AutoPipelineOptions:       .AutoPipelineOptions,
		UnstableResp3:             .UnstableResp3,
		PushNotificationProcessor: .PushNotificationProcessor,
		// Note: MaintNotificationsConfig not supported for FailoverOptions
	}
}

// Simple returns basic options created from the universal options.
func ( *UniversalOptions) () *Options {
	 := "127.0.0.1:6379"
	if len(.Addrs) > 0 {
		 = .Addrs[0]
	}

	return &Options{
		Addr:       ,
		ClientName: .ClientName,
		Dialer:     .Dialer,
		OnConnect:  .OnConnect,

		DB:                           .DB,
		Protocol:                     .Protocol,
		Username:                     .Username,
		Password:                     .Password,
		CredentialsProvider:          .CredentialsProvider,
		CredentialsProviderContext:   .CredentialsProviderContext,
		StreamingCredentialsProvider: .StreamingCredentialsProvider,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:        .DialTimeout,
		DialerRetries:      .DialerRetries,
		DialerRetryTimeout: .DialerRetryTimeout,
		ReadTimeout:        .ReadTimeout,
		WriteTimeout:       .WriteTimeout,

		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		ReadBufferSize:  .ReadBufferSize,
		WriteBufferSize: .WriteBufferSize,

		PipelineReadBufferSize:  .PipelineReadBufferSize,
		PipelineWriteBufferSize: .PipelineWriteBufferSize,
		PipelinePoolSize:        .PipelinePoolSize,

		PoolFIFO:              .PoolFIFO,
		PoolSize:              .PoolSize,
		MaxConcurrentDials:    .MaxConcurrentDials,
		PoolTimeout:           .PoolTimeout,
		MinIdleConns:          .MinIdleConns,
		MaxIdleConns:          .MaxIdleConns,
		MaxActiveConns:        .MaxActiveConns,
		ConnMaxIdleTime:       .ConnMaxIdleTime,
		ConnMaxLifetime:       .ConnMaxLifetime,
		ConnMaxLifetimeJitter: .ConnMaxLifetimeJitter,

		TLSConfig: .TLSConfig,

		DisableIdentity:           .DisableIdentity,
		DisableIndentity:          .DisableIndentity,
		IdentitySuffix:            .IdentitySuffix,
		AutoPipelineOptions:       .AutoPipelineOptions,
		UnstableResp3:             .UnstableResp3,
		PushNotificationProcessor: .PushNotificationProcessor,
		MaintNotificationsConfig:  .MaintNotificationsConfig,
		ClientSideCacheConfig:     .ClientSideCacheConfig,
		ClientSideCache:           .ClientSideCache,
		ClientSideCacheStrategy:   .ClientSideCacheStrategy,

		ClientSideCacheRefreshOnInvalidate:     .ClientSideCacheRefreshOnInvalidate,
		ClientSideCacheCoalesceMisses:          .ClientSideCacheCoalesceMisses,
		ClientSideCacheInvalidationBatchWindow: .ClientSideCacheInvalidationBatchWindow,
	}
}

// --------------------------------------------------------------------

// UniversalClient is an abstract client which - based on the provided options -
// 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.
type UniversalClient interface {
	Cmdable
	AddHook(Hook)
	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
	// AutoPipeline / AsyncAutoPipeline return an AutoPipeliner for the concrete
	// client. Supported on *Client (including sentinel-backed failover clients)
	// and *ClusterClient; *Ring returns an error (not supported).
	//
	// EXPERIMENTAL: this API is subject to change, use with caution.
	AutoPipeline() (*AutoPipeliner, error)
	AutoPipelineWithOptions(config *AutoPipelineOptions) (*AutoPipeliner, error)
	AsyncAutoPipeline() (*AutoPipeliner, error)
	AsyncAutoPipelineWithOptions(config *AutoPipelineOptions) (*AutoPipeliner, error)
	Subscribe(ctx context.Context, channels ...string) *PubSub
	PSubscribe(ctx context.Context, channels ...string) *PubSub
	SSubscribe(ctx context.Context, channels ...string) *PubSub
	Close() error
	PoolStats() *PoolStats
}

var (
	_ UniversalClient = (*Client)(nil)
	_ UniversalClient = (*ClusterClient)(nil)
	_ UniversalClient = (*Ring)(nil)
	// AutoPipeliner is a drop-in for the real clients; non-data operations
	// delegate to the underlying client.
	_ UniversalClient = (*AutoPipeliner)(nil)
)

// NewUniversalClient returns a new multi client. The type of the returned client depends
// on the following conditions:
//
//  1. If the MasterName option is specified with RouteByLatency, RouteRandomly or IsClusterMode,
//     a FailoverClusterClient is returned.
//  2. If the MasterName option is specified without RouteByLatency, RouteRandomly or IsClusterMode,
//     a sentinel-backed FailoverClient is returned.
//  3. If the number of Addrs is two or more, or IsClusterMode option is specified,
//     a ClusterClient is returned.
//  4. Otherwise, a single-node Client is returned.
//
// Passing nil UniversalOptions will cause a panic.
func ( *UniversalOptions) UniversalClient {
	if  == nil {
		panic("redis: NewUniversalClient nil options")
	}

	switch {
	case .MasterName != "" && (.RouteByLatency || .RouteRandomly || .IsClusterMode):
		return NewFailoverClusterClient(.Failover())
	case .MasterName != "":
		return NewFailoverClient(.Failover())
	case len(.Addrs) > 1 || .IsClusterMode:
		return NewClusterClient(.Cluster())
	default:
		return NewClient(.Simple())
	}
}