circuitbreaker

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

	// ErrTooManyRequests is returned when circuit breaker is half-open and max requests exceeded
	ErrTooManyRequests = errors.New("too many requests in half-open state")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker prevents cascading failures

func NewCircuitBreaker

func NewCircuitBreaker(name string, settings Settings) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(fn func() error) error

Execute runs the given function if circuit breaker is closed or half-open

func (*CircuitBreaker) GetCounts

func (cb *CircuitBreaker) GetCounts() Counts

GetCounts returns current counts

func (*CircuitBreaker) GetState

func (cb *CircuitBreaker) GetState() State

GetState returns current state

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset resets circuit breaker to closed state

type Counts

type Counts struct {
	Requests             uint32
	TotalSuccesses       uint32
	TotalFailures        uint32
	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

Counts holds circuit breaker statistics

type Settings

type Settings struct {
	// MaxRequests is the maximum number of requests allowed in half-open state
	MaxRequests uint32

	// Interval is the cyclic period in closed state to clear internal counts
	Interval time.Duration

	// Timeout is the period in open state before switching to half-open
	Timeout time.Duration

	// ReadyToTrip determines if circuit should trip to open state
	ReadyToTrip func(counts Counts) bool

	// OnStateChange is called when state changes
	OnStateChange func(name string, from State, to State)
}

Settings configures circuit breaker behavior

type State

type State int

State represents circuit breaker state

const (
	// StateClosed allows all requests
	StateClosed State = iota

	// StateOpen blocks all requests
	StateOpen

	// StateHalfOpen allows limited requests to test recovery
	StateHalfOpen
)

func (State) String

func (s State) String() string

String returns string representation of state

Jump to

Keyboard shortcuts

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