circuitbreaker

package
v3.1.17 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package circuitbreaker implements the circuit breaker pattern for external service calls. This prevents cascading failures by temporarily blocking requests to failing services.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircuitOpen     = errors.New("circuit breaker is open")
	ErrTooManyRequests = errors.New("too many requests in half-open state")
)

Common errors

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker implements the circuit breaker pattern

func New

func New(config Config) *CircuitBreaker

New creates a new circuit breaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(ctx context.Context, fn func(context.Context) error) error

Execute runs the given function if the circuit allows

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset forces the circuit breaker back to closed state

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the current state

func (*CircuitBreaker) Stats

func (cb *CircuitBreaker) Stats() Stats

Stats returns the current statistics

type Config

type Config struct {
	// Name identifies this circuit breaker
	Name string

	// MaxFailures is the number of failures before opening the circuit
	MaxFailures int

	// SuccessThreshold is the number of successes needed to close from half-open
	SuccessThreshold int

	// Timeout is how long the circuit stays open before testing
	Timeout time.Duration

	// MaxConcurrentInHalfOpen is the max concurrent requests when half-open
	MaxConcurrentInHalfOpen int

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

Config configures the circuit breaker behavior

func DefaultConfig

func DefaultConfig(name string) Config

DefaultConfig returns a sensible default configuration

type Registry

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

Registry manages multiple circuit breakers

func GetRegistry

func GetRegistry() *Registry

GetRegistry returns the default circuit breaker registry

func (*Registry) Get

func (r *Registry) Get(name string, config ...Config) *CircuitBreaker

Get returns a circuit breaker by name, creating it if necessary

func (*Registry) ResetAll

func (r *Registry) ResetAll()

ResetAll resets all circuit breakers

func (*Registry) Stats

func (r *Registry) Stats() map[string]Stats

Stats returns stats for all circuit breakers

type State

type State int

State represents the circuit breaker state

const (
	// StateClosed means the circuit is closed and requests flow through
	StateClosed State = iota

	// StateOpen means the circuit is open and requests are blocked
	StateOpen

	// StateHalfOpen means the circuit is testing if the service has recovered
	StateHalfOpen
)

func (State) String

func (s State) String() string

type Stats

type Stats struct {
	Name            string    `json:"name"`
	State           string    `json:"state"`
	Failures        int       `json:"failures"`
	Successes       int       `json:"successes"`
	LastFailure     time.Time `json:"last_failure,omitzero"`
	LastStateChange time.Time `json:"last_state_change"`
	Generation      uint64    `json:"generation"`
}

Stats returns statistics about the circuit breaker

Source Files

  • circuitbreaker.go

Jump to

Keyboard shortcuts

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