circuit

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 4 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// NumBuckets is the number of buckets used for time-based sliding window.
	NumBuckets = 10
)

Variables

View Source
var (
	// ErrCircuitOpen is returned when the circuit breaker is in the Open state.
	ErrCircuitOpen = errors.New("circuit breaker is open")
)

Functions

This section is empty.

Types

type Breaker

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

Breaker implements a circuit breaker state machine with sliding windows.

func New

func New(cfg Config) *Breaker

New returns a new Breaker with the provided configuration.

func (*Breaker) Execute

func (b *Breaker) Execute(ctx context.Context, action func() error) error

Execute wraps an action with the circuit breaker logic.

func (*Breaker) Health added in v1.0.6

func (b *Breaker) Health() <-chan StateEvent

Health returns a channel that emits state change events. The channel is created lazily on first access. The caller should use a select with default to handle non-blocking receive.

func (*Breaker) Reset added in v1.0.4

func (b *Breaker) Reset()

Reset manually resets the circuit breaker to Closed state and clears the window.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current state of the breaker.

type Config

type Config struct {
	// WindowType specifies the type of sliding window.
	WindowType WindowType

	// WindowSize is the size of the count-based sliding window.
	// Default is 100.
	WindowSize uint32

	// WindowDuration is the duration of the time-based sliding window.
	// Default is 60 seconds.
	WindowDuration time.Duration

	// FailureRateThreshold is the failure rate percentage (0.0 to 100.0).
	// If failures/total >= FailureRateThreshold/100, the circuit opens.
	// Default is 50.0.
	FailureRateThreshold float64

	// MinimumCalls is the minimum number of calls in the current window before the failure rate is calculated.
	// Default is 10.
	MinimumCalls uint64

	// ResetTimeout is the duration to wait in Open state before moving to Half-Open.
	// Default is 60 seconds.
	ResetTimeout time.Duration

	// HalfOpenMaxCalls is the number of calls allowed in Half-Open state.
	// Default is 10.
	HalfOpenMaxCalls uint64
}

Config defines the configuration for the circuit breaker.

type HealthState added in v1.0.6

type HealthState int

State represents the health state of a resilience component.

const (
	// StateHealthy represents a healthy state where requests are allowed.
	HealthStateHealthy HealthState = iota
	// StateDegraded represents a degraded but functional state.
	HealthStateDegraded
	// StateUnhealthy represents an unhealthy state (e.g., circuit open).
	HealthStateUnhealthy
)

type State

type State int

State represents the health state of the circuit breaker.

const (
	// StateClosed represents a healthy state where requests are allowed.
	StateClosed State = iota
	// StateOpen represents a failed state where requests are fast-failed.
	StateOpen
	// StateHalfOpen represents a state where a limited number of probes are allowed.
	StateHalfOpen
)

type StateEvent added in v1.0.6

type StateEvent struct {
	Component string
	State     HealthState
	Timestamp time.Time
	Message   string
}

StateEvent represents a state change event emitted by resilience components.

type WindowType added in v1.0.4

type WindowType int

WindowType specifies whether the window is count-based or time-based.

const (
	// WindowCountBased uses the last N calls to calculate failure rate.
	WindowCountBased WindowType = iota
	// WindowTimeBased uses calls in the last N duration to calculate failure rate.
	WindowTimeBased
)

Jump to

Keyboard shortcuts

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