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 ¶
var ( DefaultHalfOpenPercentages = []float64{0.1, 0.3, 0.5, 0.75, 1.0} DefaultInterMediatoryStateChangeInterval = time.Second * 1 )
Functions ¶
This section is empty.
Types ¶
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) 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) ZeroState ¶
func (cb *CircuitBreaker) ZeroState()
type HttpRequester ¶
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