circuitbreaker

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultThreshold is the default number of consecutive failures before
	// the circuit breaker opens.
	DefaultThreshold = 5

	// DefaultTimeout is the default duration the circuit breaker stays open
	// before attempting to close again.
	DefaultTimeout = 1 * time.Minute
)

Variables

This section is empty.

Functions

func SetTimeNow

func SetTimeNow(f func() time.Time) func()

SetTimeNow sets the time function for the package and returns a function to restore it. This is intended for testing purposes only.

Types

type CircuitBreaker

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

CircuitBreaker implements a simple circuit breaker pattern for service health. It tracks consecutive failures and opens the circuit after a threshold is reached.

func New

func New(threshold int, timeout time.Duration) *CircuitBreaker

New creates a new circuit breaker.

func (*CircuitBreaker) AllowRequest

func (cb *CircuitBreaker) AllowRequest() bool

AllowRequest checks if the circuit breaker allows a request to go through. It handles the state transition from Open to Half-Open.

func (*CircuitBreaker) ForceOpen

func (cb *CircuitBreaker) ForceOpen()

ForceOpen forces the circuit breaker into an open state. This is useful for testing or degraded mode initialization.

func (*CircuitBreaker) IsOpen

func (cb *CircuitBreaker) IsOpen() bool

IsOpen returns true if the circuit breaker is currently open.

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure increments the failure count.

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a success, resetting the failure count and closing the circuit if it was open or half-open.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the current state of the circuit breaker.

type State

type State int

State represents the state of the circuit breaker.

const (
	// StateClosed means requests are allowed.
	StateClosed State = iota
	// StateOpen means requests are blocked.
	StateOpen
	// StateHalfOpen means a limited number of requests are allowed to test backend health.
	StateHalfOpen
)

func (State) String

func (s State) String() string

String returns the string representation of the state.

Jump to

Keyboard shortcuts

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