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.
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 )
DefaultResponseValidator considers any status code less than 400 to be a success, from the perspective of a client. All other codes are failures.
DefaultStatusCodeValidator considers any status code less than 500 to be a success, from the perspective of a server. All other codes are failures.
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.
Middleware produces an http.Handler factory like Handler to be composed.
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(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.
Breaker is an interface representing the ability to conditionally allow requests to pass, and to report on the result of passed requests.
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.
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.