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)
Index ¶
- Constants
- Variables
- func ErrIsOpen(err error) bool
- func ErrIsTooManyRequests(err error) bool
- type Action
- type Breaker
- type Config
- type Counts
- type NowProvider
- type OnStateChangeHandler
- type Option
- func OptClosedExpiryInterval(interval time.Duration) Option
- func OptConfig(cfg Config) Option
- func OptHalfOpenMaxActions(maxActions int64) Option
- func OptNowProvider(provider NowProvider) Option
- func OptOnStateChange(handler OnStateChangeHandler) Option
- func OptOpenAction(action Action) Option
- func OptOpenExpiryInterval(interval time.Duration) Option
- func OptShouldOpenProvider(provider ShouldOpenProvider) Option
- type ShouldOpenProvider
- type State
Constants ¶
const ( DefaultClosedExpiryInterval = 5 * time.Second DefaultOpenExpiryInterval = 60 * time.Second DefaultHalfOpenMaxActions = 1 DefaultConsecutiveFailures = 5 )
Constants
Variables ¶
var ( // ErrTooManyRequests is returned when the CB state is half open and the requests count is over the cb maxRequests ErrTooManyRequests ex.Class = "too many requests" // ErrOpenState is returned when the CB state is open ErrOpenState ex.Class = "circuit breaker is open" )
Functions ¶
func ErrIsTooManyRequests ¶
ErrIsTooManyRequests returns if the error is an ErrTooManyRequests.
Types ¶
type Breaker ¶
type Breaker struct { sync.Mutex // OpenAction is an optional action to be called when the breaker is open (i.e. preventing calls // to the main action handler.) OpenAction Action // OnStateChange is an optional handler called when the breaker transitions state. OnStateChange OnStateChangeHandler // ShouldOpenProvider is called optionally to determine if we should open the breaker. ShouldOpenProvider ShouldOpenProvider // NowProvider lets you optionally inject the current time for testing. NowProvider NowProvider // HalfOpenMaxActions is the maximum number of requests // we can make when the state is HalfOpen. HalfOpenMaxActions int64 // ClosedExpiryInterval is the cyclic period of the closed state for the CircuitBreaker to clear the internal Counts. // If Interval is 0, the CircuitBreaker doesn't clear internal Counts during the closed state. ClosedExpiryInterval time.Duration // OpenExpiryInterval is the period of the open state, // after which the state of the CircuitBreaker becomes half-open. // If Timeout is 0, the timeout value of the CircuitBreaker is set to 60 seconds. OpenExpiryInterval time.Duration // Counts are stats for the breaker. Counts Counts // contains filtered or unexported fields }
Breaker is a state machine to prevent performing actions that are likely to fail.
type Config ¶
type Config struct { HalfOpenMaxActions int64 `json:"halfOpenMaxActions" yaml:"halfOpenMaxActions"` ClosedExpiryInterval time.Duration `json:"closedExpiryInterval" yaml:"closedExpiryInterval"` OpenExpiryInterval time.Duration `json:"openExpiryInterval" yaml:"openExpiryInterval"` }
Config is the breaker config.
type Counts ¶
type Counts struct { Requests int64 `json:"requests"` TotalSuccesses int64 `json:"totalSuccesses"` TotalFailures int64 `json:"totalFailures"` ConsecutiveSuccesses int64 `json:"consecutiveSuccesses"` ConsecutiveFailures int64 `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 OnStateChangeHandler ¶
OnStateChangeHandler is called when the state changes.
type Option ¶
Option is a mutator for a breaker.
func OptClosedExpiryInterval ¶
OptClosedExpiryInterval sets the ClosedExpiryInterval.
func OptHalfOpenMaxActions ¶
OptHalfOpenMaxActions sets the HalfOpenMaxActions.
func OptNowProvider ¶
func OptNowProvider(provider NowProvider) Option
OptNowProvider sets the now provider on the breaker.
func OptOnStateChange ¶
func OptOnStateChange(handler OnStateChangeHandler) Option
OptOnStateChange sets the OnStateChange handler on the breaker.
func OptOpenAction ¶
OptOpenAction sets the open action on the breaker.
func OptOpenExpiryInterval ¶
OptOpenExpiryInterval sets the OpenExpiryInterval.
func OptShouldOpenProvider ¶
func OptShouldOpenProvider(provider ShouldOpenProvider) Option
OptShouldOpenProvider sets the ShouldCloseProvider provider on the breaker.
type ShouldOpenProvider ¶
ShouldOpenProvider returns if the breaker should open.