circuitbreaker

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

ErrCircuitOpen is returned by Execute when the circuit breaker is open and the request was rejected without calling the handler.

Functions

func DefaultIsFailure

func DefaultIsFailure(statusCode int) bool

DefaultIsFailure returns true for standard HTTP status codes that typically indicate upstream overload.

func StateToGaugeValue

func StateToGaugeValue(s gobreaker.State) float64

StateToGaugeValue converts gobreaker.State to a gauge value. closed=0, half-open=0.5, open=1

Types

type ProviderCircuitBreakers

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

ProviderCircuitBreakers manages per-endpoint/model circuit breakers for a single provider.

func NewProviderCircuitBreakers

func NewProviderCircuitBreakers(provider string, cfg *config.CircuitBreaker, onChange func(endpoint, model string, from, to gobreaker.State), m *metrics.Metrics) *ProviderCircuitBreakers

NewProviderCircuitBreakers creates circuit breakers for a single provider. Returns nil if cfg is nil (no circuit breaker protection). onChange is called when circuit state changes. metrics is used to record circuit breaker reject counts (can be nil).

func (*ProviderCircuitBreakers) Execute

func (p *ProviderCircuitBreakers) Execute(endpoint, model string, w http.ResponseWriter, handler func(http.ResponseWriter) error) error

Execute runs the given handler function within circuit breaker protection. If the circuit is open, the request is rejected with a 503 response, metrics are recorded, and ErrCircuitOpen is returned. Otherwise, it returns the handler's error (or nil on success). The handler receives a wrapped ResponseWriter that captures the status code. If the receiver is nil (no circuit breaker configured), the handler is called directly.

func (*ProviderCircuitBreakers) Get

func (p *ProviderCircuitBreakers) Get(endpoint, model string) *gobreaker.CircuitBreaker[struct{}]

Get returns the circuit breaker for an endpoint/model tuple, creating it if needed.

func (*ProviderCircuitBreakers) OpenErrorResponse

func (p *ProviderCircuitBreakers) OpenErrorResponse() []byte

OpenErrorResponse returns the error response body when the circuit is open. This is exposed for handlers to use when responding to rejected requests.

func (*ProviderCircuitBreakers) Provider

func (p *ProviderCircuitBreakers) Provider() string

Provider returns the provider name for this circuit breaker.

func (*ProviderCircuitBreakers) Timeout

func (p *ProviderCircuitBreakers) Timeout() time.Duration

Timeout returns the configured timeout duration for this circuit breaker.

Jump to

Keyboard shortcuts

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