Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

multidb

v9.24.1
github.com/redis/go-redis/v9/multidb

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

53 matches

Top ↑

Package multidb provides health checks (ping, lag-aware) and health-check policies (all, majority, any) for use with redis.MultiDBClient.

EXPERIMENTAL: this API is subject to change, use with caution.

Constants

DefaultHealthCheckProbes (3 declarations)

const (
	DefaultHealthCheckProbes  = imultidb.DefaultProbes       // 3
	DefaultHealthCheckDelay   = imultidb.DefaultProbeDelay   // 500ms
	DefaultHealthCheckTimeout = imultidb.DefaultProbeTimeout // 3s
)

Default values for health check configuration

DefaultRESTAPIPort (3 declarations)

const (
	DefaultRESTAPIPort  = 9443
	DefaultLagTolerance = 5000 // milliseconds
	DefaultHTTPTimeout  = 10 * time.Second
)

Default values for LagAwareHealthCheck

Types

ConfigurableHealthCheck

type ConfigurableHealthCheck interface {
	redis.MultiDBHealthCheck
	Config() HealthCheckConfig
}

ConfigurableHealthCheck is an interface for health checks that have configuration.

HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for making HTTP requests.

HealthCheckConfig

type HealthCheckConfig = redis.MultiDBHealthCheckConfig

HealthCheckConfig holds configuration for health checks. It is the same type as redis.MultiDBHealthCheckConfig, so the default MultiDBClient policy honors it too.

DefaultHealthCheckConfig

func DefaultHealthCheckConfig() HealthCheckConfig

DefaultHealthCheckConfig returns the default health check configuration.

HealthCheckOption

type HealthCheckOption func(*HealthCheckConfig)

HealthCheckOption is a functional option for configuring health checks.

WithDelay

func WithDelay(delay time.Duration) HealthCheckOption

WithDelay sets the delay between probes. See WithProbes for the timeout cap.

WithProbes

func WithProbes(probes int) HealthCheckOption

WithProbes sets the number of probes. Every policy honors it, including the default redis.MultiDBClient policy. A check still running when MultiDBOptions.HealthCheckTimeout (default 3s; lowered to HealthCheckInterval/2 when not below the interval) expires fails, so keep (Probes-1)*Delay plus the probe time below it. Under the default policy NewMultiDBClient and AddDatabase reject a check whose (Probes-1)*Delay is not below that timeout or the check's own Timeout. Under the default policy SetActiveDatabase and ProbeTargetBeforeFailover run one probe.

WithTimeout

func WithTimeout(timeout time.Duration) HealthCheckOption

WithTimeout sets the timeout for the health check (all its probes). It is capped by MultiDBOptions.HealthCheckTimeout.

HealthCheckPolicy

type HealthCheckPolicy interface {
	Execute(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.Client) bool
	ExecuteCluster(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.ClusterClient) bool
}

HealthCheckPolicy defines how health check probes are evaluated.

HealthyAllPolicy

type HealthyAllPolicy struct{}

HealthyAllPolicy returns true if ALL probes succeed for each health check. For each health check, it runs the configured number of probes with delays. ALL probes must succeed for the health check to be considered healthy.

Note: The policy applies to probes within each individual health check. When multiple health checks are provided (e.g. Ping + LagAware), ALL health checks must pass — the policy does not control the relationship between different health checks, only between probes of the same check.

NewHealthyAllPolicy

func NewHealthyAllPolicy() *HealthyAllPolicy

HealthyAllPolicy.Execute

func (p *HealthyAllPolicy) Execute(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.Client) bool

HealthyAllPolicy.ExecuteCluster

func (p *HealthyAllPolicy) ExecuteCluster(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.ClusterClient) bool

HealthyAnyPolicy

type HealthyAnyPolicy struct{}

HealthyAnyPolicy returns true if AT LEAST ONE probe succeeds. For each health check, it runs probes until one succeeds or all fail.

Note: The policy applies to probes within each individual health check. When multiple health checks are provided, ALL health checks must pass (each needing at least one successful probe).

NewHealthyAnyPolicy

func NewHealthyAnyPolicy() *HealthyAnyPolicy

HealthyAnyPolicy.Execute

func (p *HealthyAnyPolicy) Execute(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.Client) bool

HealthyAnyPolicy.ExecuteCluster

func (p *HealthyAnyPolicy) ExecuteCluster(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.ClusterClient) bool

HealthyMajorityPolicy

type HealthyMajorityPolicy struct{}

HealthyMajorityPolicy returns true if a MAJORITY of probes succeed. For each health check, it runs the configured number of probes with delays. More than half of the probes must succeed.

Note: The policy applies to probes within each individual health check. When multiple health checks are provided, ALL health checks must pass (each needing a majority of its probes to succeed).

NewHealthyMajorityPolicy

func NewHealthyMajorityPolicy() *HealthyMajorityPolicy

HealthyMajorityPolicy.Execute

func (p *HealthyMajorityPolicy) Execute(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.Client) bool

HealthyMajorityPolicy.ExecuteCluster

func (p *HealthyMajorityPolicy) ExecuteCluster(ctx context.Context, checks []redis.MultiDBHealthCheck, client *redis.ClusterClient) bool

LagAwareHealthCheck

type LagAwareHealthCheck struct {
	// contains filtered or unexported fields
}

LagAwareHealthCheck checks database health via Redis Enterprise REST API. It verifies that the database is healthy based on replication lag tolerance.

NewLagAwareHealthCheck

func NewLagAwareHealthCheck(opts ...LagAwareHealthCheckOption) *LagAwareHealthCheck

NewLagAwareHealthCheck creates a new LagAwareHealthCheck.

Generic health check settings (probes, delay, timeout) can be supplied via WithLagAwareHealthCheckConfig.

LagAwareHealthCheck.CheckClusterHealth

func (h *LagAwareHealthCheck) CheckClusterHealth(ctx context.Context, client *redis.ClusterClient) (bool, error)

CheckClusterHealth performs a single REST API health check probe. The cluster's currently discovered shard addresses are tried first, then the configured seeds: the cluster can be healthy and routing through live nodes while the (possibly stale) startup seeds are unreachable.

LagAwareHealthCheck.CheckHealth

func (h *LagAwareHealthCheck) CheckHealth(ctx context.Context, client *redis.Client) (bool, error)

CheckHealth performs a single REST API health check probe. It returns (false, err) with the error that made the database unhealthy (config error, an unusable address, or a REST API failure) so callers can record it.

LagAwareHealthCheck.Config

func (h *LagAwareHealthCheck) Config() HealthCheckConfig

LagAwareHealthCheck.FailbackOnly

func (h *LagAwareHealthCheck) FailbackOnly() bool

FailbackOnly reports that this check's verdict may only gate routing traffic TO a member — candidate probes before failover, auto-fallback and initial selection — and must never evict the current active. Replication lag is a fail-back criterion (a recovered member must not take traffic back before it has caught up); on the member already serving traffic it is not an availability signal, and the REST API may itself be unreachable precisely when the member is failing. Failover decisions ride on traffic signals (the failure detector and circuit breaker) and liveness checks. The MultiDB background health loop skips FailbackOnly checks when probing the active member; if that leaves no check, it runs the default PING instead, so the active always keeps a liveness probe. A member with only FailbackOnly checks is therefore PING-gated while active and lag-gated as a failover or fallback candidate.

LagAwareHealthCheckOption

type LagAwareHealthCheckOption func(*LagAwareHealthCheck)

LagAwareHealthCheckOption is a functional option for LagAwareHealthCheck.

WithLagAwareBaseURL

func WithLagAwareBaseURL(baseURL string) LagAwareHealthCheckOption

WithLagAwareBaseURL sets the base URL for the REST API. By default the check derives it from the member's own database host, which the local endpoint-availability probe relies on: the /v1/local/ API answers for the node that serves the request. A custom base URL must therefore point at (or resolve to) the cluster node hosting this member's endpoint — routing it through a shared admin address, proxy, or load balancer makes the check report the availability of whichever node the URL happens to reach.

For a cluster member the same limit applies to every master: a fixed URL cannot reach each master's own node, so the check runs ONCE against the configured URL instead of once per master and reports the node it reaches. Leave the base URL unset to get the per-master check.

WithLagAwareBasicAuth

func WithLagAwareBasicAuth(username, password string) LagAwareHealthCheckOption

WithLagAwareBasicAuth sets basic authentication credentials.

WithLagAwareClientCert

func WithLagAwareClientCert(certPEM, keyPEM []byte) LagAwareHealthCheckOption

WithLagAwareClientCert sets the client certificate for mutual TLS (mTLS). If the key pair cannot be loaded, the error is recorded and subsequent health checks fail rather than silently disabling mTLS.

WithLagAwareClientCertFromFiles

func WithLagAwareClientCertFromFiles(certFile, keyFile string) LagAwareHealthCheckOption

WithLagAwareClientCertFromFiles loads client cert and key from files. If the key pair cannot be loaded, the error is recorded and subsequent health checks fail rather than silently disabling mTLS.

WithLagAwareHTTPClient

func WithLagAwareHTTPClient(client HTTPClient) LagAwareHealthCheckOption

WithLagAwareHTTPClient sets a custom HTTP client.

WithLagAwareHealthCheckConfig

func WithLagAwareHealthCheckConfig(opts ...HealthCheckOption) LagAwareHealthCheckOption

WithLagAwareHealthCheckConfig applies generic HealthCheckOption values (e.g. WithProbes, WithDelay, WithTimeout) to a LagAwareHealthCheck.

WithLagAwareInsecureSkipVerify

func WithLagAwareInsecureSkipVerify() LagAwareHealthCheckOption

WithLagAwareInsecureSkipVerify disables TLS certificate verification.

WithLagAwareRESTAPIPort

func WithLagAwareRESTAPIPort(port int) LagAwareHealthCheckOption

WithLagAwareRESTAPIPort sets the REST API port (default: 9443).

WithLagAwareRootCAs

func WithLagAwareRootCAs(certPEM []byte) LagAwareHealthCheckOption

WithLagAwareRootCAs sets the root CA certificates for TLS verification. If the PEM data cannot be parsed, the error is recorded and subsequent health checks fail rather than silently running without the CAs.

WithLagAwareRootCAsFromFile

func WithLagAwareRootCAsFromFile(caFile string) LagAwareHealthCheckOption

WithLagAwareRootCAsFromFile loads root CA certificates from a PEM file. If the file cannot be read or parsed, the error is recorded and subsequent health checks fail rather than silently running without the CAs.

WithLagAwareTLSConfig

func WithLagAwareTLSConfig(cfg *tls.Config) LagAwareHealthCheckOption

WithLagAwareTLSConfig sets a custom TLS configuration. The config is cloned so later options (and the health check itself) never mutate the caller's value. A nil config clears any previously set TLS configuration.

WithLagAwareTolerance

func WithLagAwareTolerance(toleranceMS int) LagAwareHealthCheckOption

WithLagAwareTolerance sets the lag tolerance in milliseconds (default: 5000).

PingHealthCheck

type PingHealthCheck struct {
	// contains filtered or unexported fields
}

PingHealthCheck checks health using the PING command. Each call to CheckHealth/CheckClusterHealth performs a SINGLE ping probe. The number of probes and how they're interpreted is controlled by the HealthCheckPolicy and the config (Probes, Delay, Timeout).

NewPingHealthCheck

func NewPingHealthCheck(opts ...HealthCheckOption) *PingHealthCheck

NewPingHealthCheck creates a new PingHealthCheck with optional configuration.

Example:

hc := multidb.NewPingHealthCheck()

hc := multidb.NewPingHealthCheck(
    multidb.WithProbes(5),
    multidb.WithDelay(100*time.Millisecond),
    multidb.WithTimeout(5*time.Second),
)

PingHealthCheck.CheckClusterHealth

func (h *PingHealthCheck) CheckClusterHealth(ctx context.Context, client *redis.ClusterClient) (bool, error)

CheckClusterHealth performs a single PING probe against every MASTER in the cluster. Replicas are deliberately not probed: MultiDB rejects the replica-routing options (RouteByLatency/RouteRandomly), so member traffic only ever reaches masters — a dead replica must not fail the member and trigger a failover away from a cluster that is serving normally. It returns (false, err) with the first master's PING error when the probe fails. An empty topology (no masters pinged) is reported as unhealthy rather than trivially healthy, matching LagAwareHealthCheck which fails when the cluster has no addresses.

PingHealthCheck.CheckHealth

func (h *PingHealthCheck) CheckHealth(ctx context.Context, client *redis.Client) (bool, error)

CheckHealth performs a single PING probe against the client. It returns (false, err) with the PING error when the probe fails so callers can record why the check was unhealthy.

PingHealthCheck.Config

func (h *PingHealthCheck) Config() HealthCheckConfig

Config returns the health check configuration.

Dependency graph