circuitbreaker

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCircuitOpen is returned when the state of Circuit Breaker is open.
	ErrCircuitOpen = errors.New("circuit breaker is open")
)

Functions

This section is empty.

Types

type Action

type Action func() (any, error)

Action function in CircuitBreaker.

type CircuitBreaker

type CircuitBreaker struct {
	OnSuccess func() // OnSuccess will be called when Action can be processed, when state is StateClosed or StateHalfOpen
	OnFailure func() // OnFailure will be triggered when CircuitBreaker moved to StateOpen and Action failed to execute and will return error.
	// contains filtered or unexported fields
}

CircuitBreaker component that is designed to prevent sending execution that are likely to fail.

func NewCircuitBreaker

func NewCircuitBreaker(cfg Configuration) (*CircuitBreaker, error)

NewCircuitBreaker creates a new CircuitBreaker instance with the specified configuration.

func (*CircuitBreaker) GetState

func (cb *CircuitBreaker) GetState() State

GetState returns current state of the Circuit Breaker.

func (*CircuitBreaker) Proceed

func (cb *CircuitBreaker) Proceed(action Action) (any, error)

Proceed function in CircuitBreaker

type Configuration

type Configuration struct {
	MaxFailuresThreshold string `json:"cb_max_failures_threshold,omitempty"`
	ResetTimeout         string `json:"cb_reset_timeout,omitempty"`
}

Configuration for CircuitBreaker:

MaxFailuresThreshold Maximum number of failures allowed.

ResetTimeout in seconds, is the period of the open state. After which the state of the CircuitBreaker becomes half-open. fields can be used for example as ENV variables in your project like MY_APP_CB_MAX_FAILURES_THRESHOLD:"3"

type State

type State byte

State is a type that represents a state of CircuitBreaker.

const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

These constants are states of CircuitBreaker.

func (State) String

func (s State) String() string

String implements stringer interface.

Jump to

Keyboard shortcuts

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