Help us shape the future of Go! Take the 2026 Go Developer Survey

redisconn

package module
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package redisconn constructs provider-neutral Redis clients for standalone, cluster, and Sentinel deployments.

Index

Examples

Constants

View Source
const (
	DefaultDialTimeout  = 5 * time.Second
	DefaultReadTimeout  = 3 * time.Second
	DefaultWriteTimeout = 3 * time.Second
)

Default client timeouts applied when the corresponding Config field is zero.

Variables

This section is empty.

Functions

func BuildTLSConfig

func BuildTLSConfig(cfg *TLSConfig) (*tls.Config, error)

BuildTLSConfig builds a TLS 1.2-or-newer configuration. A nil input means plaintext; a nil CA bundle uses system roots.

func NewClient

func NewClient(ctx context.Context, cfg *Config) (goredis.UniversalClient, error)

NewClient constructs and verifies a Redis client. The caller's configuration is not mutated. A client that fails its initial PING is closed.

Example (BoundedPool)
package main

import (
	"context"

	"github.com/stacklok/toolhive-core/redisconn"
)

func main() {
	client, err := redisconn.NewClient(context.Background(), &redisconn.Config{
		Addr:           "redis.example.com:6379",
		PoolSize:       8,
		MaxActiveConns: 12,
		TLS:            &redisconn.TLSConfig{},
	})
	if err != nil {
		return
	}
	defer client.Close()
}
Example (StaticCredentials)
package main

import (
	"context"

	"github.com/stacklok/toolhive-core/redisconn"
)

func main() {
	client, err := redisconn.NewClient(context.Background(), &redisconn.Config{
		Addr:     "redis.example.com:6379",
		Username: "application",
		Password: "secret",
		TLS:      &redisconn.TLSConfig{}, // system roots and server verification
	})
	if err != nil {
		return
	}
	defer client.Close()
}

Types

type Config

type Config struct {
	// Addr is the host:port for standalone or cluster mode.
	Addr string
	// ClusterMode enables Redis Cluster and requires Addr.
	ClusterMode bool
	// SentinelConfig selects Sentinel mode and is mutually exclusive with Addr.
	SentinelConfig *SentinelConfig
	// Username and Password are static Redis credentials. Password is mutually
	// exclusive with DynamicAuth.
	Username    string
	Password    string //nolint:gosec // field name, not a hardcoded credential
	DynamicAuth *DynamicAuth
	// DB is ignored in cluster mode.
	DB int
	// Zero timeouts use the package defaults.
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	// PoolSize is the base number of socket connections in each pool. In
	// cluster mode, it applies per node. Zero uses the go-redis default.
	PoolSize int
	// MaxActiveConns is the maximum number of connections allocated by each
	// pool. In cluster mode, it applies per node. Zero allows the pool to
	// allocate connections without a hard limit.
	MaxActiveConns int
	// TLS secures Redis data-node connections; SentinelTLS independently secures
	// Sentinel discovery connections.
	TLS         *TLSConfig
	SentinelTLS *TLSConfig
	// ConnMaxLifetime overrides DynamicAuth.ConnMaxLifetime when non-zero.
	ConnMaxLifetime time.Duration
}

Config configures a Redis client. Exactly one of Addr or SentinelConfig must be set. ClusterMode upgrades an Addr-based config to Redis Cluster.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks connection topology, TLS, and dynamic-auth safety.

type CredentialsProvider

type CredentialsProvider func(context.Context) (username, password string, err error)

CredentialsProvider resolves credentials for a new Redis connection.

type DynamicAuth

type DynamicAuth struct {
	// CredentialsProviderContext returns credentials for each new connection.
	CredentialsProviderContext CredentialsProvider
	// ConnMaxLifetime is the provider-recommended maximum connection age. A
	// non-zero Config.ConnMaxLifetime overrides it.
	ConnMaxLifetime time.Duration
	// AllowInsecureTransport permits dynamic credentials without verified TLS.
	// Use only when a trusted local tunnel supplies transport security.
	AllowInsecureTransport bool
}

DynamicAuth configures credentials that are resolved while each new connection is initialized. ConnMaxLifetime retires over-age connections lazily on reuse; it does not proactively refresh or reauthenticate them.

type SentinelConfig

type SentinelConfig struct {
	// MasterName is the monitored Redis master name.
	MasterName string
	// SentinelAddrs lists Sentinel host:port endpoints.
	SentinelAddrs []string
}

SentinelConfig identifies a Sentinel-managed Redis master.

type TLSConfig

type TLSConfig struct {
	// InsecureSkipVerify disables server certificate verification.
	InsecureSkipVerify bool
	// CACert is an optional PEM CA bundle; empty uses system roots.
	CACert []byte
	// ClientCert and ClientKey are a PEM mTLS certificate pair.
	ClientCert []byte
	ClientKey  []byte
}

TLSConfig configures server verification, custom roots, and mutual TLS.

Directories

Path Synopsis
aws module
azure module
gcp module

Jump to

Keyboard shortcuts

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