circuitbreaker

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package circuitbreaker provides a per-host circuit breaker as HTTP middleware.

The circuit breaker monitors request failures and temporarily blocks requests to unhealthy hosts, allowing them time to recover before retrying.

State machine

  • Closed — normal operation, requests pass through
  • Open — too many failures, requests are rejected with ErrCircuitOpen
  • HalfOpen — after a cooldown period, one probe request is allowed through

Usage

mw := circuitbreaker.Transport(
    circuitbreaker.WithThreshold(5),
    circuitbreaker.WithTimeout(30 * time.Second),
)
transport := mw(http.DefaultTransport)

The circuit breaker is per-host: each unique request host gets its own independent breaker state machine stored in a sync.Map.

Sentinel errors

ErrCircuitOpen is returned when a request is rejected because the circuit is in the Open state.

Index

Constants

This section is empty.

Variables

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

ErrCircuitOpen is returned by Allow when the breaker is in the Open state.

Functions

func Transport

func Transport(opts ...Option) middleware.Middleware

Transport returns a middleware that applies per-host circuit breaking. It maintains an internal map of host → *Breaker so each target host is tracked independently.

Types

type Breaker

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

Breaker implements a per-endpoint circuit breaker state machine.

State transitions:

Closed   → Open:     after failureThreshold consecutive failures
Open     → HalfOpen: after openDuration passes
HalfOpen → Closed:   on success
HalfOpen → Open:     on failure (timer resets)

func NewBreaker

func NewBreaker(opts ...Option) *Breaker

NewBreaker creates a Breaker with the given options.

func (*Breaker) Allow

func (b *Breaker) Allow() (done func(success bool), err error)

Allow checks whether a request is permitted. If allowed it returns a done callback that the caller MUST invoke with the result of the request. If the breaker is open, it returns ErrCircuitOpen.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current state of the breaker.

type Option

type Option func(*options)

Option configures a Breaker.

func WithFailureThreshold

func WithFailureThreshold(n int) Option

WithFailureThreshold sets the number of consecutive failures required to trip the breaker from Closed to Open. Default is 5.

func WithHalfOpenMax

func WithHalfOpenMax(n int) Option

WithHalfOpenMax sets the maximum number of concurrent probe requests allowed while the breaker is in the HalfOpen state. Default is 1.

func WithOpenDuration

func WithOpenDuration(d time.Duration) Option

WithOpenDuration sets how long the breaker stays in the Open state before transitioning to HalfOpen. Default is 30s.

type State

type State int

State represents the current state of a circuit breaker.

const (
	StateClosed   State = iota // normal operation
	StateOpen                  // failing, reject requests
	StateHalfOpen              // testing recovery
)

func (State) String

func (s State) String() string

String returns a human-readable name for the state.

Jump to

Keyboard shortcuts

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