Search and commands

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

Type to search packages.

failuredetector

v9.24.1
github.com/redis/go-redis/v9/internal/failuredetector

Documentation

pkg.go.dev ↗

Table of contents

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

11 matches

Top ↑

Package failuredetector provides primitives for deciding when a Redis database is unhealthy enough that the multi-database client should trigger a failover.

Types

CommandFailureDetector

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

CommandFailureDetector observes command outcomes inside a sliding time window and reports when failover should be triggered. The implementation uses a fixed-size ring of buckets and only sync/atomic operations on the hot path, so RecordSuccess and RecordFailure scale across many goroutines without contention.

NewCommandFailureDetector

func NewCommandFailureDetector(config CommandFailureDetectorConfig) *CommandFailureDetector

NewCommandFailureDetector creates a new sliding-window failure detector with the given configuration. Any zero-valued field in config is replaced with its documented default, so passing the zero value is equivalent to passing DefaultCommandFailureDetectorConfig().

CommandFailureDetector.RecordFailure

func (d *CommandFailureDetector) RecordFailure(err error)

RecordFailure records a failed command outcome. A nil error is treated as a no-op (so callers that forward errors unconditionally do not accumulate phantom failures); context cancellation and deadline-exceeded errors are also ignored because they originate on the client side and are not a signal about the database's health.

CommandFailureDetector.RecordSuccess

func (d *CommandFailureDetector) RecordSuccess()

RecordSuccess records a successful command outcome.

CommandFailureDetector.Reset

func (d *CommandFailureDetector) Reset()

Reset discards all recorded outcomes. Concurrent recorders may race with Reset; in the worst case a small number of in-flight increments survive the reset, which is acceptable for a failure detector.

CommandFailureDetector.ShouldFailover

func (d *CommandFailureDetector) ShouldFailover() bool

ShouldFailover returns true when the outcomes observed within the trailing FailureDetectionWindow indicate that failover should be triggered. A database is considered faulty when at least MinNumFailures commands have failed AND the observed failure rate is at least FailureRateThreshold. Either half of the check can be disabled by setting IgnoreMinNumFailures or IgnoreFailureRateThreshold; when both are disabled, any single failure in the window triggers failover. At least one failure must have been observed for failover to be considered.

CommandFailureDetector.Stats

func (d *CommandFailureDetector) Stats() (successes, failures uint64)

Stats returns a read-only snapshot of the outcomes observed within the current sliding window. The returned counts are aggregated across the bucket ring and reflect the same view of state used by ShouldFailover.

CommandFailureDetectorConfig

type CommandFailureDetectorConfig struct {
	// MinNumFailures is the minimum number of failed commands that must be
	// observed within the detection window before failover is considered.
	// Ignored when IgnoreMinNumFailures is true.
	// Default: 1000.
	MinNumFailures uint64

	// IgnoreMinNumFailures disables the MinNumFailures check, so ShouldFailover
	// considers only FailureRateThreshold. Use this when the rate alone is the
	// signal you trust (typically combined with a small FailureRateThreshold).
	IgnoreMinNumFailures bool

	// FailureRateThreshold is the failure rate (0.0-1.0] that, together with
	// MinNumFailures, triggers failover. For example, 0.1 means failover when
	// 10% or more of the commands in the window fail.
	// Ignored when IgnoreFailureRateThreshold is true.
	// Default: 0.1. (A zero value means "use the default".)
	FailureRateThreshold float64

	// IgnoreFailureRateThreshold disables the FailureRateThreshold check, so
	// ShouldFailover considers only MinNumFailures. Use this when the absolute
	// number of failures is the signal you trust regardless of traffic volume.
	IgnoreFailureRateThreshold bool

	// FailureDetectionWindow is the sliding time window over which command
	// outcomes are considered. Outcomes older than FailureDetectionWindow
	// from now are no longer counted by ShouldFailover.
	// Default: 2 seconds.
	FailureDetectionWindow time.Duration

	// NumBuckets controls the time resolution of the sliding window. The
	// window is divided into NumBuckets sub-buckets, each of width
	// FailureDetectionWindow / NumBuckets, and outcomes age out one bucket
	// at a time. Larger values give finer-grained ageing at the cost of
	// O(NumBuckets) work per ShouldFailover call.
	// Default: 10.
	NumBuckets int
}

CommandFailureDetectorConfig configures CommandFailureDetector. Every field has a documented default that NewCommandFailureDetector applies when the field is left at its zero value, so a zero-valued config is a valid way to ask for the recommended defaults.

DefaultCommandFailureDetectorConfig

func DefaultCommandFailureDetectorConfig() CommandFailureDetectorConfig

DefaultCommandFailureDetectorConfig returns the default configuration. NewCommandFailureDetector applies the same defaults to any zero-valued field, so this is mainly useful as a starting point for tuning.

FailureDetector

type FailureDetector interface {
	// RecordSuccess records a successful command outcome.
	RecordSuccess()
	// RecordFailure records a failed command outcome. Implementations may
	// ignore errors that are not health signals (for example client-side
	// context cancellation).
	RecordFailure(err error)
	// ShouldFailover returns true when the recent outcomes indicate that
	// failover should be triggered.
	ShouldFailover() bool
	// Reset discards all observed outcomes and starts fresh.
	Reset()
}

FailureDetector decides when failover should be triggered based on a stream of command outcomes observed by the caller.

Dependency graph