circuitbreaker

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 3 Imported by: 0

README

Circuit breaker

This is a package for circuit breaking.

Usage

Here is an example of how to use this package.

action := func() error {
       // function code to be executed
}

cb := NewCircuitBreaker(Options{}) // with default options
err := cb.Execute(action)

if err != nil {
	t.Error("error should be nil")
}

These are the options you can set and their default values:

type Options struct {
	// Number of failed requests required to set the state to open
	FailureThreshold int
	// Number of successful requests required to set the state to closed
	RecoveryThreshold int
	// Number of concurrent test requests allowed in half open state
	TestRequestsAllowed int
	// Timeout for open state after which the state is set to half open
	ResetTimeout time.Duration
	// Interval after which the failure count is reset
	FailureResetInterval time.Duration
}

const (
	DefaultFailureThreshold     = 5
	DefaultRecoveryThreshold    = 3
	DefaultTestRequestsAllowed  = 3
	DefaultResetTimeout         = 5 * time.Second
	DefaultFailureResetInterval = 60 * time.Second
)

Documentation

Index

Constants

View Source
const (
	DefaultFailureThreshold     = 5
	DefaultRecoveryThreshold    = 3
	DefaultTestRequestsAllowed  = 3
	DefaultResetTimeout         = 5 * time.Second
	DefaultFailureResetInterval = 60 * time.Second
)

Variables

View Source
var (
	ErrorCircuitOpen                    = errors.New("circuit breaker is open")
	ErrorCircuitHalfOpenAllowedRequests = errors.New("circuit breaker half open test requests exhausted")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

func NewCircuitBreaker

func NewCircuitBreaker(opts Options) *CircuitBreaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(action func() error) error

Execute runs the action and returns an error if the circuit breaker is open

type Options

type Options struct {
	// Number of failed requests required to set the state to open
	FailureThreshold int
	// Number of successful requests required to set the state to closed
	RecoveryThreshold int
	// Number of concurrent test requests allowed in half open state
	TestRequestsAllowed int
	// Timeout for open state after which the state is set to half open
	ResetTimeout time.Duration
	// Interval after which the failure count is reset
	FailureResetInterval time.Duration
}

type State

type State int
const (
	Closed State = iota
	Open
	HalfOpen
)

Jump to

Keyboard shortcuts

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