handy: github.com/streadway/handy/breaker Index | Files

package breaker

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

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.

Index

Package Files

breaker.go doc.go handler.go metrics.go transport.go

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func NewBreaker Uses

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 Uses

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 Uses

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

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

type ResponseValidator Uses

type ResponseValidator func(*http.Response) bool

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 StatusCodeValidator Uses

type StatusCodeValidator func(int) bool

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.

Package breaker imports 5 packages (graph) and is imported by 60 packages. Updated 2019-08-20. Refresh now. Tools for package owners.