breaker - ActiveState ActiveGo 1.8
...

Package breaker

import "github.com/streadway/handy/breaker"
Overview
Index

Overview ▾

Package breaker implements a circuit breaker with configurable failure thresholds. It provides a Handler, to circuit break in HTTP servers, and a Transport, to circuit break in HTTP clients.

Constants

const (
    // DefaultWindow is the default number of per-second buckets that will be
    // considered when calculating metrics on the circuit breaker.
    DefaultWindow = 5 * time.Second

    // DefaultCooldown is the default period a circuit will remain in the open
    // state before allowing a single sentinel request through.
    DefaultCooldown = 1 * time.Second

    // DefaultMinObservations is the default number of observations that must
    // be made before the circuit breaker
    DefaultMinObservations = 10
)

Variables

var (
    // ErrCircuitOpen is returned by the transport when the downstream is
    // unavailable due to a broken circuit.
    ErrCircuitOpen = errors.New("circuit open")
)

func DefaultResponseValidator

func DefaultResponseValidator(resp *http.Response) bool

DefaultResponseValidator considers any status code less than 400 to be a success, from the perspective of a client. All other codes are failures.

func DefaultStatusCodeValidator

func DefaultStatusCodeValidator(code int) bool

DefaultStatusCodeValidator considers any status code less than 500 to be a success, from the perspective of a server. All other codes are failures.

func Handler

func Handler(breaker Breaker, validator StatusCodeValidator, next http.Handler) http.Handler

Handler produces an http.Handler that's governed by the passed Breaker and StatusCodeValidator. Responses written by the next http.Handler whose status codes fail the validator signal failures to the breaker. Once the breaker opens, incoming requests are terminated before being forwarded with HTTP 503.

func Middleware

func Middleware(breaker Breaker, validator StatusCodeValidator) func(http.Handler) http.Handler

Middleware produces an http.Handler factory like Handler to be composed.

func NewBreaker

func NewBreaker(failureRatio float64) breaker

NewBreaker constructs a new circuit breaker, initially closed. The breaker opens after failureRatio failures per success, and only after DefaultMinObservations have been made.

func Transport

func Transport(breaker Breaker, validator ResponseValidator, next http.RoundTripper) http.RoundTripper

Transport produces an http.RoundTripper that's governed by the passed Breaker and ResponseValidator. Responses that fail the validator signal failures to the breaker. Once the breaker opens, outgoing requests are terminated before being forwarded with ErrCircuitOpen.

type Breaker

Breaker is an interface representing the ability to conditionally allow requests to pass, and to report on the result of passed requests.

type Breaker interface {
    Allow() bool
    Success(time.Duration)
    Failure(time.Duration)
}

type ResponseValidator

ResponseValidator is a function that determines if an http.Response received by a circuit breaking Transport should count as a success or a failure. The DefaultResponseValidator can be used in most situations.

type ResponseValidator func(*http.Response) bool

type StatusCodeValidator

StatusCodeValidator is a function that determines if a status code written to a client by a circuit breaking Handler should count as a success or failure. The DefaultStatusCodeValidator can be used in most situations.

type StatusCodeValidator func(int) bool