Documentation
¶
Overview ¶
Package redisconn constructs provider-neutral Redis clients for standalone, cluster, and Sentinel deployments.
Index ¶
Examples ¶
Constants ¶
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 ¶
BuildTLSConfig builds a TLS 1.2-or-newer configuration. A nil input means plaintext; a nil CA bundle uses system roots.
func NewClient ¶
NewClient constructs and verifies a Redis client. The caller's configuration is not mutated. A client that fails its initial PING is closed.
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()
}
Output:
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
// 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.
type CredentialsProvider ¶
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.