Documentation
¶
Overview ¶
Package breaker provides a circuitbraker mechanism for dealing with flaky or unreliable counterparties.
The algorithm used for the state machine is described by Microsoft https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589784(v=pandp.10)
An example of using a circuit breaker for an http call might be:
b := circuitbreaker.New[string, *http.Response](circuitbreaker.Config{}) phoneHome := b.Intercept(async.ActionerFunc(func(ctx context.Context, url string) (*http.Response, error) { return http.DefaultClient.Do(http.NewRequestWithContext(ctx, http.VerbGet, url, nil)) })
In the above, `phoneHome` now will be wrapped with circuit breaker mechanics. You would call it with `phoneHome.Action(ctx, nil)` etc.
Index ¶
Constants ¶
const ( DefaultOpenExpiryInterval = 60 * time.Second DefaultClosedFailureExpiryInterval = 10 * time.Second DefaultHalfOpenMaxActions uint64 = 5 DefaultFailureThreshold uint64 = 5 )
Constants
Variables ¶
var ( // ErrTooManyRequests is returned when the CB state is half open and the requests count is over the cb maxRequests ErrTooManyRequests errutil.Class = "too many requests" // ErrOpenState is returned when the CB state is open ErrOpenState errutil.Class = "circuit breaker is open" )
Functions ¶
func ErrIsTooManyRequests ¶
ErrIsTooManyRequests returns if the error is an ErrTooManyRequests.
Types ¶
type ActionFunc ¶
ActionFunc is a function that implements action.
type Breaker ¶
type Breaker[A, B any] struct { Action Action[A, B] OpenAction Action[A, B] OnStateChange OnStateChangeFunc ShouldOpen ShouldOpenFunc[A] Now NowFunc Config Config // contains filtered or unexported fields }
Breaker is a state machine to prevent performing actions that are likely to fail.
func (*Breaker[A, B]) EvaluateState ¶
EvaluateState returns the current state of the CircuitBreaker.
This method is a kind of idirect because can't know for sure what the state is at a given time without evaluating expiration times and potentially calling handlers if the state changes after an expiry.
As a result this method takes a context, and may call the `OnStateChange` delegate value if it's set.
type Config ¶
type Config struct { FailureThreshold uint64 `json:"failureThreshold" yaml:"failureThreshold"` HalfOpenMaxActions uint64 `json:"halfOpenMaxActions" yaml:"halfOpenMaxActions"` OpenExpiryInterval time.Duration `json:"openExpiryInterval" yaml:"openExpiryInterval"` ClosedFailureExpiryInterval time.Duration `json:"closedFailureExpiryInterval" yaml:"closedFailureExpiryInterval"` }
Config is the breaker config.
func (Config) ClosedFailureExpiryIntervalOrDefault ¶
func (Config) FailureThresholdOrDefault ¶
func (Config) HalfOpenMaxActionsOrDefault ¶
func (Config) OpenExpiryIntervalOrDefault ¶
type Counts ¶
type Counts struct { Requests uint64 `json:"requests"` TotalSuccesses uint64 `json:"totalSuccesses"` TotalFailures uint64 `json:"totalFailures"` ConsecutiveSuccesses uint64 `json:"consecutiveSuccesses"` ConsecutiveFailures uint64 `json:"consecutiveFailures"` }
Counts holds the numbers of requests and their successes/failures. CircuitBreaker clears the internal Counts either on the change of the state or at the closed-state intervals. Counts ignores the results of the requests sent before clearing.
type NoopAction ¶
type NoopAction[A, B any] struct{}
NoopAction is an actioner type that does nothing.
type OnStateChangeFunc ¶
OnStateChangeFunc is called when the state changes.
type Option ¶
func OptOpenAction ¶
func OptShouldOpen ¶
func OptShouldOpen[A, B any](shouldOpenFunc ShouldOpenFunc[A]) Option[A, B]
type ShouldOpenFunc ¶
ShouldOpenFunc returns if the breaker should open.