circuitbreaker

package module
v0.0.0-...-0fab27a Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

circuitbreaker package provides a simple circuit breaker implementation that receives http connection pool and returns a Breaker struct

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultHalfOpenPercentages               = []float64{0.1, 0.3, 0.5, 0.75, 1.0}
	DefaultInterMediatoryStateChangeInterval = time.Second * 1
)
View Source
var (
	ErrRateTooHigh      = errors.New("error rate too high")
	ErrRequestDropped   = errors.New("request dropped early by circuit breaker")
	ErrThresholdTooHigh = errors.New("threshold too high")
)

Functions

This section is empty.

Types

type Bucket

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

type CircuitBreaker

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

func NewCircuitBreaker

func NewCircuitBreaker(windowInSeconds, bucketsPerSecond int,
	threshold float64, stateStepInterval time.Duration, req HttpRequester) *CircuitBreaker

NewCircuitBreaker creates a new CircuitBreaker with the given windowInSeconds, bucketPerSecond and breakigThreshold. The windowInSeconds is the total time in seconds that the CircuitBreaker will keep track of. The bucketPerSecond is the number of buckets that the windowInSeconds will be divided into. The breakigThreshold is the percentage of failures that will cause the CircuitBreaker to open. The StateHandler is the handler that will be used to evaluate the state of the CircuitBreaker.

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

func (*CircuitBreaker) DoRequest

func (cb *CircuitBreaker) DoRequest(req *http.Request) (resp *http.Response, err error)

func (*CircuitBreaker) Execute

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

MakeRequest registers a request and a failure in the current bucket. It then updates the stats and evaluates the state of the CircuitBreaker. If the CircuitBreaker is in the Open state, it will return an error.

Client is responisble for handling the error and determining which errors should be counted as error for circuit breaker e.x:

err := cb.MakeRequest(&cb.RetryPolicy{Count: 3, Wailt: time.Second*3},func() error {
	res, err := http.Get("http://example.com")
	if err != nil {
		return err
	}

	// check the status code and return an error if it is not 200
	if !(res.StatusCode >= 200 && res.StatusCode < 400){
		return errors.New("server returned non-200 status code")
	}

	// read response body
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	// if you want to ignore bad response value for circuit breaker, thats up to you
	if err != nil {
		return nil
	}
	return nil
})

func (*CircuitBreaker) StateEval

func (cb *CircuitBreaker) StateEval()

Bring everuything here

func (*CircuitBreaker) ZeroState

func (cb *CircuitBreaker) ZeroState()

type HttpRequester

type HttpRequester interface {
	Do(req *http.Request) (*http.Response, error)
}

HttpRequester is the interface abstracting http.Client if you want to use monitoring and tracing, just implement them on http.Client and provide it to the breaker

type RetryPolicy

type RetryPolicy struct {
	Count int
	Wait  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