circuit

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 4 Imported by: 0

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) 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 State

type State int

State represents the current 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 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