Documentation ¶
Overview ¶
Circuit breakers start in the Standby state first, observing responses and watching location metrics.
Once the Circuit breaker condition is met, it enters the "Tripped" state, where it activates fallback scenario for all requests during the FallbackDuration time period and reset the stats for the location.
After FallbackDuration time period passes, Circuit breaker enters "Recovering" state, during that state it will start passing some traffic back to the endpoints, increasing the amount of passed requests using linear function:
allowedRequestsRatio = 0.5 * (Now() - StartRecovery())/RecoveryDuration
Two scenarios are possible in the "Recovering" state: 1. Condition matches again, this will reset the state to "Tripped" and reset the timer. 2. Condition does not match, circuit breaker enters "Standby" state
It is possible to define actions (e.g. webhooks) of transitions between states:
* OnTripped action is called on transition (Standby -> Tripped) * OnStandby action is called on transition (Recovering -> Standby)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustParseExpression ¶
MustParseExpresison calls ParseExpression and panics if expression is incorrect, for use in tests
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker is a middleware that implements circuit breaker pattern
func New ¶
func New(condition threshold.Predicate, fallback middleware.Middleware, options Options) (*CircuitBreaker, error)
New creates a new CircuitBreaker middleware
func (*CircuitBreaker) ProcessRequest ¶
ProcessRequest is called on every request to the endpoint. CircuitBreaker uses this feature to intercept the request if it's in Tripped state or slowly start passing the requests to endpoint if it's in the recovering state
func (*CircuitBreaker) ProcessResponse ¶
func (c *CircuitBreaker) ProcessResponse(r request.Request, a request.Attempt)
func (*CircuitBreaker) String ¶
func (c *CircuitBreaker) String() string
String returns log-friendly representation of the circuit breaker state
type Options ¶
type Options struct { // Check period is how frequently circuit breaker checks for the condition to match CheckPeriod time.Duration // FallbackDuration is a period for fallback scenario FallbackDuration time.Duration // RecoveryDuration is a period for recovery scenario RecoveryDuration time.Duration // TimeProvider is a interface to freeze time in tests TimeProvider timetools.TimeProvider // OnTripped defines action activated during (Standby->Tripped) transition OnTripped SideEffect // OnTripped defines action activated during (Recovering->Standby) transition OnStandby SideEffect }
Options defines optional parameters for CircuitBreaker
type RedirectFallback ¶
type RedirectFallback struct {
// contains filtered or unexported fields
}
func NewRedirectFallback ¶
func NewRedirectFallback(r Redirect) (*RedirectFallback, error)
func (*RedirectFallback) ProcessRequest ¶
func (*RedirectFallback) ProcessResponse ¶
func (f *RedirectFallback) ProcessResponse(r request.Request, a request.Attempt)
type ResponseFallback ¶
type ResponseFallback struct {
// contains filtered or unexported fields
}
func NewResponseFallback ¶
func NewResponseFallback(r Response) (*ResponseFallback, error)
func (*ResponseFallback) ProcessRequest ¶
func (*ResponseFallback) ProcessResponse ¶
func (f *ResponseFallback) ProcessResponse(r request.Request, a request.Attempt)
type SideEffect ¶
type SideEffect interface {
Exec() error
}
type WebhookSideEffect ¶
type WebhookSideEffect struct {
// contains filtered or unexported fields
}
func NewWebhookSideEffect ¶
func NewWebhookSideEffect(w Webhook) (*WebhookSideEffect, error)
func (*WebhookSideEffect) Exec ¶
func (w *WebhookSideEffect) Exec() error