client

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *redis.Client

Types

type Option

type Option func(*redis.Options)

func WithAddr

func WithAddr(addr string) Option

Addr is the address formated as host:port

func WithClientName

func WithClientName(clientName string) Option

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

func WithConnMaxIdleTime

func WithConnMaxIdleTime(connMaxIdleTime time.Duration) Option

ConnMaxIdleTime is the maximum amount of time a connection may be idle. Should be less than server's timeout.

Expired connections may be closed lazily before reuse. If d <= 0, connections are not closed due to a connection's idle time. -1 disables idle timeout check.

default: 30 minutes

func WithConnMaxLifetime

func WithConnMaxLifetime(connMaxLifetime time.Duration) Option

ConnMaxLifetime is the maximum amount of time a connection may be reused.

Expired connections may be closed lazily before reuse. If <= 0, connections are not closed due to a connection's age.

default: 0

func WithContextTimeoutEnabled

func WithContextTimeoutEnabled(contextTimeoutEnabled bool) Option

ContextTimeoutEnabled controls whether the client respects context timeouts and deadlines. See https://redis.uptrace.dev/guide/go-redis-debugging.html#timeouts

func WithCredentialsProvider

func WithCredentialsProvider(credentialsProvider func() (username string, password string)) Option

CredentialsProvider allows the username and password to be updated before reconnecting. It should return the current username and password.

func WithCredentialsProviderContext

func WithCredentialsProviderContext(credentialsProviderContext func(ctx context.Context) (username string, password string, err error)) Option

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.

func WithDB

func WithDB(db int) Option

DB is the database to be selected after connecting to the server.

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) Option

DialTimeout for establishing new connections.

default: 5 seconds

func WithDialer

func WithDialer(dialer func(ctx context.Context, network, addr string) (net.Conn, error)) Option

Dialer creates new network connection and has priority over Network and Addr options.

func WithDisableIdentity

func WithDisableIdentity(disableIdentity bool) Option

DisableIdentity is used to disable CLIENT SETINFO command on connect.

default: false

func WithFailingTimeoutSeconds

func WithFailingTimeoutSeconds(failingTimeoutSeconds int) Option

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. Default is 15 seconds.

func WithIdentitySuffix

func WithIdentitySuffix(identitySuffix string) Option

Add suffix to client name. Default is empty. IdentitySuffix - add suffix to client name.

func WithLimiter

func WithLimiter(limiter redis.Limiter) Option

Limiter interface used to implement circuit breaker or rate limiter.

func WithMaxActiveConns

func WithMaxActiveConns(maxActiveConns int) Option

MaxActiveConns is the maximum number of connections allocated by the pool at a given time. When zero, there is no limit on the number of connections in the pool. If the pool is full, the next call to Get() will block until a connection is released.

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

MaxIdleConns is the maximum number of idle connections. The idle connections are not closed by default.

default: 0

func WithMaxRetries

func WithMaxRetries(maxRetries int) Option

MaxRetries is the maximum number of retries before giving up. -1 (not 0) disables retries.

default: 3 retries

func WithMaxRetryBackoff

func WithMaxRetryBackoff(maxRetryBackoff time.Duration) Option

MaxRetryBackoff is the maximum backoff between each retry. -1 disables backoff. default: 512 milliseconds;

func WithMinIdleConns

func WithMinIdleConns(minIdleConns int) Option

MinIdleConns is the minimum number of idle connections which is useful when establishing new connection is slow. The idle connections are not closed by default.

default: 0

func WithMinRetryBackoff

func WithMinRetryBackoff(minRetryBackoff time.Duration) Option

MinRetryBackoff is the minimum backoff between each retry. -1 disables backoff.

default: 8 milliseconds

func WithNetwork

func WithNetwork(network string) Option

Network type, either tcp or unix.

default: is tcp.

func WithOnConnect

func WithOnConnect(onConnect func(ctx context.Context, cn *redis.Conn) error) Option

Hook that is called when new connection is established.

func WithPassword

func WithPassword(password string) Option

Password is an optional password. Must match the password specified in the `requirepass` server configuration option (if connecting to a Redis 5.0 instance, or lower), or the User Password when connecting to a Redis 6.0 instance, or greater, that is using the Redis ACL system.

func WithPoolFIFO

func WithPoolFIFO(poolFIFO bool) Option

PoolFIFO type of connection pool.

  • true for FIFO pool
  • false for LIFO pool.

Note that FIFO has slightly higher overhead compared to LIFO, but it helps closing idle connections faster reducing the pool size.

func WithPoolSize

func WithPoolSize(poolSize int) Option

PoolSize is the base number of socket connections. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS. If there is not enough connections in the pool, new connections will be allocated in excess of PoolSize, you can limit it through MaxActiveConns

default: 10 * runtime.GOMAXPROCS(0)

func WithPoolTimeout

func WithPoolTimeout(poolTimeout time.Duration) Option

PoolTimeout is the amount of time client waits for connection if all connections are busy before returning an error.

default: ReadTimeout + 1 second

func WithProtocol

func WithProtocol(protocol int) Option

Protocol 2 or 3. Use the version to negotiate RESP version with redis-server.

default: 3.

func WithReadBufferSize

func WithReadBufferSize(readBufferSize int) Option

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)

func WithReadTimeout

func WithReadTimeout(readTimeout time.Duration) Option

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: 3 seconds

func WithStreamingCredentialsProvider

func WithStreamingCredentialsProvider(streamingCredentialsProvider auth.StreamingCredentialsProvider) Option

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.

func WithTLSConfig

func WithTLSConfig(tlsConfig *tls.Config) Option

TLSConfig to use. When set, TLS will be negotiated.

func WithUnstableResp3

func WithUnstableResp3(unstableResp3 bool) Option

UnstableResp3 enables Unstable mode for Redis Search module with RESP3. When unstable mode is enabled, the client will use RESP3 protocol and only be able to use RawResult

func WithUsername

func WithUsername(username string) Option

Username is used to authenticate the current connection with one of the connections defined in the ACL list when connecting to a Redis 6.0 instance, or greater, that is using the Redis ACL system.

func WithWriteBufferSize

func WithWriteBufferSize(writeBufferSize int) Option

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)

func WithWriteTimeout

func WithWriteTimeout(writeTimeout time.Duration) Option

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: 3 seconds

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL