breaker

package
v0.0.0-...-d356a15 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

ErrBreakerOpen indicates that the breaker is in the open state and will not run the supplied callback. This error will also be returned in the half open state and at capacity.

Functions

func WithIsFailure

func WithIsFailure(isFailure func(err error) bool) option

WithIsFailure defines the logic for determining if the error should be counted toward the breaker failures.

func WithMaxFailures

func WithMaxFailures(maxFailures uint) option

WithMaxFailures indicates the breaker should go into the open state after reaching the supplied number of consecutive failures.

func WithMaxHalfOpenRequests

func WithMaxHalfOpenRequests(maxRequests uint) option

WithMaxRequests set the maximum number of requests that should be made while in the half open state.

func WithNow

func WithNow(now func() time.Time) option

WithNow sets the function for getting the current time. This is only useful for testing.

func WithTimeout

func WithTimeout(timeout time.Duration) option

WithTimeout sets the duration over which the breaker will remain in the open state.

Types

type Breaker

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

Breaker provides the logic for conditionally calling a function based on its passed performance.

func New

func New(opts ...option) *Breaker

New creates a new Breaker with the supplied options.

func (*Breaker) Run

func (b *Breaker) Run(cb func() error) (err error)

Run executes the callback if the circuit breaker is not in the open state. It will track successes and failures in order to determine the state.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current breaker state.

type Registry

type Registry map[string]Breaker

Registry stores circuit breakers by name. This is useful because a circuit breaker must be re-used for each call of the same type and the registry provides a mechanism for retrieving that circuit breaker.

func NewRegistry

func NewRegistry() Registry

Creates a new circuit breaker registry.

func (Registry) Get

func (r Registry) Get(name string) (Breaker, bool)

Gets the supplied circuit breaker using its name. The second return value indicates whether it was found.

func (Registry) Register

func (r Registry) Register(name string, b Breaker) error

Register associates the supplied circuit breaker with the name and stores in the registry.

type State

type State struct {

	// The number of consecutive failures. This is only tracked when the
	// circuit breaker is in the closed state.
	Failures uint

	// The number of consecutive successes. This is only tracked when
	// the circuit breaker is in the half open state.
	Successes uint
	// contains filtered or unexported fields
}

State maintains the state of the circuit breaker, which includes the status and relevant counts.

func (State) Status

func (s State) Status() Status

Status returns circuit breaker status.

type Status

type Status uint8
const (
	// Closed indicates the callback function will be run as normal.
	Closed Status = 0

	// HalfOpen indicates the callback function will be run in a limited
	// capacity and a failure will return it to the open state.
	HalfOpen Status = 1

	// Open indicates the callback function will not be run while in
	// this state.
	Open Status = 2
)

func (Status) String

func (s Status) String() string

Jump to

Keyboard shortcuts

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