circuit_breaker

package module
v0.0.0-...-f71b3c2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 4 Imported by: 0

README

Circuit Breaker

Implements circuit-breaker pattern with Golang

Inpsired by sony/gobreaker

Dependencies

Architecture

Diagram

Example

func main() {
    config := circuit_breaker.Config{
		Name: "Circuit Breaker",
		ReadyToTrip: func(counts circuit_breaker.Counts) bool {
			failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
			return counts.Requests >= 3 && failureRatio >= 0.7
		},
		OnStateChange: func(name string, from, to circuit_breaker.State) {
			fmt.Printf("%s: state changed from %s to %s", name, from, to)
		},
	}

    cb := circuit_breaker.NewCircuitBreaker(config)

    msg, err := cb.Execute(func() (interface{}, error) {
	// your actual service call here...
    })
}

See example for details.

License

MIT. Please see the license file for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooManyRequests is returned when the CB state is half open and the requests count is over the cb requestThreshold
	ErrTooManyRequests = errors.New("too many requests")
	// ErrOpenState is returned when the CB state is open
	ErrOpenState = errors.New("circuit breaker is open")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

func NewCircuitBreaker

func NewCircuitBreaker(cfg Config) *CircuitBreaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(req func() (interface{}, error)) (interface{}, error)

type Config

type Config struct {
	Name             string
	RequestThreshold uint32
	Timeout          time.Duration

	ReadyToTrip   func(counts Counts) bool
	OnStateChange func(name string, from State, to State)
}

type Counts

type Counts struct {
	Requests             uint32
	TotalSuccesses       uint32
	TotalFailures        uint32
	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

type State

type State uint32
const (
	StateClosed State = iota
	StateOpen
	StateHalfOpen
)

func (State) String

func (state State) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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