circuit

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 7 Imported by: 0

README

Circuit

License GoDev Reference Go Report Card

Package circuit provides a circuit breaker implementation.

Documentation

Overview

Package circuit provides a circuit breaker implementation.

Index

Constants

View Source
const (
	DefaultWindow       = 10 * time.Second
	DefaultCooldown     = 5 * time.Second
	DefaultSpacing      = 2 * time.Second
	DefaultJitterFactor = 0.25
	DefaultMinSuccess   = 3
	DefaultMinResults   = 5
	DefaultFailureRatio = 0.5
)

Default values.

Variables

This section is empty.

Functions

This section is empty.

Types

type Breaker

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

A Breaker is a circuit breaker.

func NewBreaker

func NewBreaker(options ...Option) (*Breaker, error)

NewBreaker returns a circuit breaker with the given options.

func (*Breaker) AddNotifyFunc

func (b *Breaker) AddNotifyFunc(notify NotifyFunc)

AddNotifyFunc adds the notify function to be called in a critical section when the State of the circuit breaker changes.

func (*Breaker) Allow

func (b *Breaker) Allow() bool

Allow returns a value indicating if an operation is allowed. If the operation is allowed, its result MUST be recorded.

func (*Breaker) Record

func (b *Breaker) Record(err error)

Record records the error result of an allowed operation.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current state of the circuit breaker.

type NotifyFunc

type NotifyFunc func(state State)

A NotifyFunc is a function that is called in a critical section when the state of a circuit breaker changes.

type Observer

type Observer interface {
	// ObserverStateChange is a called in a critical section
	// when the state of the circuit breaker changes.
	ObserveStateChange(state State)
}

An Observer observes changes to circuit breakers.

type Option

type Option func(*Breaker) error

An Option provides an override to defaults.

func WithCooldown

func WithCooldown(cooldown time.Duration) Option

WithCooldown returns an Option that sets the cooldown time before probes will be allowed when the circuit breaker is Open. The default is 5s.

func WithFailureRatio

func WithFailureRatio(ratio float64) Option

WithFailureRatio returns an Option that sets the minimum failure ratio in a window required to switch the circuit breaker from Closed to Open. The default is 50%.

func WithFilter

func WithFilter(filter func(error) error) Option

WithFilter returns an Option that specifies a function to filter expected error results that should not be counted as a failure (e.g. Canceled, InvalidArgument, NotFound, etc.).

func WithJitterFactor

func WithJitterFactor(jitter float64) Option

WithJitterFactor returns an Option that sets the random jitter factor applied to cooldown and spacing delays. The default is 25%.

func WithLogger

func WithLogger(log logr.Logger) Option

WithLogger returns an Option that specifies the logger.

func WithMinResults

func WithMinResults(min int) Option

WithMinResults returns an Option that sets the minimum number of results in a window required to switch the circuit breaker from Closed to Open. The default is 5.

func WithMinSuccess

func WithMinSuccess(min int) Option

WithMinSuccess returns an Option that sets the minimum number of consecutive successful probes required to switch the circuit breaker from HalfOpen to Closed. The default is 3.

func WithNotifyFunc

func WithNotifyFunc(notify NotifyFunc) Option

WithNotifyFunc returns an Option that adds a function to be called in a critical section when the State of the circuit breaker changes.

func WithObserver

func WithObserver(observer Observer) Option

WithObserver returns an Option that adds an Observer.

func WithSpacing

func WithSpacing(spacing time.Duration) Option

WithSpacing returns an Option that sets the spacing time between allowed probes when the circuit breaker is HalfOpen. The default is 2s.

func WithWindow

func WithWindow(window time.Duration) Option

WithWindow returns an Option that sets the window of time after which results are reset when the circuit breaker is Closed. The default is 10s.

type State

type State int

State is the state of a circuit breaker.

const (
	Closed   State = iota // Closed allows all operations.
	HalfOpen              // HalfOpen allows a probing operation.
	Open                  // Open disallows all operations.
)

func (State) String

func (i State) String() string

Directories

Path Synopsis
Package circuitprom provides a Prometheus collector for circuit breakers.
Package circuitprom provides a Prometheus collector for circuit breakers.

Jump to

Keyboard shortcuts

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