Documentation
¶
Overview ¶
Package circuit implements the Circuit Breaker pattern. https://docs.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultShouldTripFunc is a default ShouldTripFunc. DefaultShouldTripFunc = func(counts Counts) bool { return counts.ConsecutiveFailures >= 3 } // DefaultShouldResetFunc is a default ShouldResetFunc. DefaultShouldResetFunc = func(counts Counts) bool { return counts.ConsecutiveSuccesses >= 3 } // DefaultBackoffDurationFunc is an exponential backoff function DefaultBackoffDurationFunc = ExponentialBackoffDuration(time.Duration(100)*time.Second, time.Duration(500)*time.Millisecond) )
Functions ¶
Types ¶
type BackoffDurationFunc ¶
BackoffDurationFunc is a function that takes in a Counts and returns the backoff duration
type BackoffHook ¶
BackoffHook is a function that represents backoff.
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is a state machine to prevent sending requests that are likely to fail.
func NewBreaker ¶
NewBreaker returns a new Breaker configured with the given Settings.
type ErrOpenState ¶
type ErrOpenState struct{}
ErrOpenState is returned when the b state is open
func (*ErrOpenState) Error ¶
func (e *ErrOpenState) Error() string
type Options ¶
type Options struct { HalfOpenConcurrentRequests int ShouldTripFunc ShouldTripFunc ShouldResetFunc ShouldResetFunc BackoffDurationFunc BackoffDurationFunc // hooks OnStateChange StateChangeHook OnBackoff BackoffHook // used in tests TestClock clock.Clock }
Options configures Breaker:
HalfOpenConcurrentRequests specifies how many concurrent requests to allow while the circuit is in the half-open state
ShouldTripFunc specifies when the circuit should trip from the closed state to the open state. It takes a Counts struct and returns a bool.
ShouldResetFunc specifies when the circuit should be reset from the half-open state to the closed state and allow all requests. It takes a Counts struct and returns a bool.
BackoffDurationFunc specifies how long to set the backoff duration. It takes a counts struct and returns a time.Duration
OnStateChange is called whenever the state of the Breaker changes.
OnBackoff is called whenever a backoff is set with the backoff duration and reset time ¶
TestClock is used to mock the clock during tests
type ShouldResetFunc ¶
ShouldResetFunc is a function that takes in a Counts and returns true if the circuit breaker should be reset.
type ShouldTripFunc ¶
ShouldTripFunc is a function that takes in a Counts and returns true if the circuit breaker should be tripped.
type StateChangeHook ¶
type StateChangeHook func(prev, to State)
StateChangeHook is a function that represents a state change.