circuitbreaker

package
v0.0.0-...-1b81d80 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// No means the service is not overloaded
	No = false
	// Yes means the service is overloaded
	Yes = true
)

Variables

View Source
var AlwaysClosedSettings = Settings{
	ErrorRateThresholdPct: 0,
	ErrorRateWindow:       10 * time.Second,
	MinQPSForOpen:         10,
	CoolDownInterval:      10 * time.Second,
	HalfOpenSuccessCount:  1,
}

AlwaysClosedSettings is a configuration that never trips the circuit breaker.

View Source
var CircuitBreakerKey = cbCtxKey{}

Key used to store circuit breaker

Functions

func WithCircuitBreaker

func WithCircuitBreaker(ctx context.Context, cb *CircuitBreaker) context.Context

WithCircuitBreaker stores the circuit breaker into a new context

Types

type CircuitBreaker

type CircuitBreaker struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CircuitBreaker is a state machine to prevent sending requests that are likely to fail.

func FromContext

func FromContext(ctx context.Context) *CircuitBreaker

FromContext retrieves the circuit breaker from the context

func NewCircuitBreaker

func NewCircuitBreaker(name string, st Settings) *CircuitBreaker

NewCircuitBreaker returns a new CircuitBreaker configured with the given Settings.

func (*CircuitBreaker) ChangeSettings

func (cb *CircuitBreaker) ChangeSettings(apply func(config *Settings))

ChangeSettings changes the CircuitBreaker settings. The changes will be reflected only in the next evaluation window.

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(call func() (Overloading, error)) error

Execute calls the given function if the CircuitBreaker is closed and returns the result of execution. Execute returns an error instantly if the CircuitBreaker is open. https://github.com/tikv/rfcs/blob/master/text/0115-circuit-breaker.md

func (*CircuitBreaker) IsEnabled

func (cb *CircuitBreaker) IsEnabled() bool

IsEnabled returns true if the circuit breaker is enabled.

type Overloading

type Overloading bool

Overloading is a type describing service return value

type Settings

type Settings struct {
	// Defines the error rate threshold to trip the circuit breaker.
	ErrorRateThresholdPct uint32
	// Defines the average qps over the `error_rate_window` that must be met before evaluating the error rate threshold.
	MinQPSForOpen uint32
	// Defines how long to track errors before evaluating error_rate_threshold.
	ErrorRateWindow time.Duration
	// Defines how long to wait after circuit breaker is open before go to half-open state to send a probe request.
	CoolDownInterval time.Duration
	// Defines how many subsequent requests to test after cooldown period before fully close the circuit.
	HalfOpenSuccessCount uint32
}

Settings describes configuration for Circuit Breaker

type State

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

State represents the state of CircuitBreaker.

type StateType

type StateType int

StateType is a type that represents a state of CircuitBreaker.

const (
	StateClosed StateType = iota
	StateOpen
	StateHalfOpen
)

States of CircuitBreaker.

func (StateType) String

func (s StateType) String() string

String implements stringer interface.

Jump to

Keyboard shortcuts

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