circuitbreaker

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2019 License: Apache-2.0 Imports: 6 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) goresilience.Runner

New returns a new circuit breaker runner.

The circuit breaker has 3 states, close, open and half open.

The circuit starts in closed state, this means that the sent funcs will be excuted, the circuit will record the results of the executed funcs.

This records will be based on a sliding window divided in buckets of a T duration (example, 10 buckets of 1s each, will record the results of the last 10s, every second a new bucket will be created and the oldest bucket of the 10 buckets will be deleted).

Being in closed state... when the error percent is greater that the configured threshold in `ErrorPercentThresholdToOpen` setting and at least it made N executions configured in `MinimumRequestToOpen` will move to open state.

Being in open state the circuit will return directly an error without executing. When the circuit has been in open state for a T duration configured in `WaitDurationInOpenState` will move to half open state.

being in half open state... the circuit will allow executing as being closed except that the measurements are different, in this case it will check that when N executions have been made (configured in `SuccessfulRequiredOnHalfOpen`) if all of them have been successfull, if all have been ok it will move to closed state, if not it will move to open state.

Note: On every state change the recorded metrics will be reset.

func NewMiddleware

func NewMiddleware(cfg Config) goresilience.Middleware

NewMiddleware returns a middleware with the runner that is return by circuitbreaker.New (see that for more information).

Types

type Config

type Config struct {
	// ErrorPercentThresholdToOpen is the error percent based on total execution requests
	// to pass from closed to open state.
	ErrorPercentThresholdToOpen int
	// MinimumRequestToOpen is the minimum quantity of execution request needed
	// to evaluate the percent of errors to allow opening the circuit.
	MinimumRequestToOpen int
	// SuccessfulRequiredOnHalfOpen are the number of request (and successes) the
	// circuitbreaker will check when is on half open state before closing the
	// circuit again.
	SuccessfulRequiredOnHalfOpen int
	// WaitDurationInOpenState is how long the circuit will be in
	// open state before moving to half open state.
	WaitDurationInOpenState time.Duration
	// Sliding window settings
	// Example: window size 10 and 1s bucket duration will store the data of the latest 10s
	// to select the state of the circuit.
	//
	// MetricsSlidingWindowBucketQuantity is the number of buckets that will have the window to
	// store the metrics. This window will delete the oldest bucket and create new
	// This way the circuit breaker only uses the latest data to get the state of the circuit.
	MetricsSlidingWindowBucketQuantity int
	// MetricsBucketDuration is the duration for a bucket to store the metrics that collects,
	// This way the circuit will have a window of N buckets of T duration each.
	MetricsBucketDuration time.Duration
}

Config is the configuration of the circuit breaker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL