Documentation ¶
Overview ¶
Package cbreaker implements circuit breaker similar to https://github.com/Netflix/Hystrix/wiki/How-it-Works
Vulcan circuit breaker watches the error condtion to match after which it activates the fallback scenario, e.g. returns the response code or redirects the request to another location
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 ¶
- type CircuitBreaker
- type Option
- func CheckPeriod(d time.Duration) Option
- func Fallback(h http.Handler) Option
- func FallbackDuration(d time.Duration) Option
- func Logger(l utils.Logger) Option
- func OnStandby(s SideEffect) Option
- func OnTripped(s SideEffect) Option
- func RecoveryDuration(d time.Duration) Option
- func Verbose(verbose bool) Option
- type Redirect
- type RedirectFallback
- type RedirectFallbackOption
- type Response
- type ResponseFallback
- type ResponseFallbackOption
- type SideEffect
- type Webhook
- type WebhookSideEffect
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker is http.Handler that implements circuit breaker pattern.
func (*CircuitBreaker) Fallback ¶
func (c *CircuitBreaker) Fallback(f http.Handler)
Fallback sets the fallback handler to be called by circuit breaker handler.
func (*CircuitBreaker) ServeHTTP ¶
func (c *CircuitBreaker) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (*CircuitBreaker) String ¶
func (c *CircuitBreaker) String() string
String returns log-friendly representation of the circuit breaker state.
func (*CircuitBreaker) Wrap ¶
func (c *CircuitBreaker) Wrap(next http.Handler)
Wrap sets the next handler to be called by circuit breaker handler.
type Option ¶
type Option func(*CircuitBreaker) error
Option represents an option you can pass to New.
func CheckPeriod ¶
CheckPeriod is how long the CircuitBreaker will wait between successive checks of the breaker condition.
func Fallback ¶
Fallback defines the http.Handler that the CircuitBreaker should route requests to when it prevents a request from taking its normal path.
func FallbackDuration ¶
FallbackDuration is how long the CircuitBreaker will remain in the Tripped state before trying to recover.
func OnStandby ¶
func OnStandby(s SideEffect) Option
OnStandby sets a SideEffect to run when entering the Standby state. Only one SideEffect can be set for this hook.
func OnTripped ¶
func OnTripped(s SideEffect) Option
OnTripped sets a SideEffect to run when entering the Tripped state. Only one SideEffect can be set for this hook.
func RecoveryDuration ¶
RecoveryDuration is how long the CircuitBreaker will take to ramp up requests during the Recovering state.
type RedirectFallback ¶
type RedirectFallback struct {
// contains filtered or unexported fields
}
RedirectFallback fallback redirect handler.
func NewRedirectFallback ¶
func NewRedirectFallback(r Redirect, options ...RedirectFallbackOption) (*RedirectFallback, error)
NewRedirectFallback creates a new RedirectFallback.
func (*RedirectFallback) ServeHTTP ¶
func (f *RedirectFallback) ServeHTTP(w http.ResponseWriter, req *http.Request)
type RedirectFallbackOption ¶
type RedirectFallbackOption func(*RedirectFallback) error
RedirectFallbackOption represents an option you can pass to NewRedirectFallback.
func RedirectFallbackDebug ¶
func RedirectFallbackDebug(debug bool) RedirectFallbackOption
RedirectFallbackDebug additional debug information.
func RedirectFallbackLogger ¶
func RedirectFallbackLogger(l utils.Logger) RedirectFallbackOption
RedirectFallbackLogger defines the logger used by ResponseFallback.
type ResponseFallback ¶
type ResponseFallback struct {
// contains filtered or unexported fields
}
ResponseFallback fallback response handler.
func NewResponseFallback ¶
func NewResponseFallback(r Response, options ...ResponseFallbackOption) (*ResponseFallback, error)
NewResponseFallback creates a new ResponseFallback.
func (*ResponseFallback) ServeHTTP ¶
func (f *ResponseFallback) ServeHTTP(w http.ResponseWriter, req *http.Request)
type ResponseFallbackOption ¶
type ResponseFallbackOption func(*ResponseFallback) error
ResponseFallbackOption represents an option you can pass to NewResponseFallback.
func ResponseFallbackDebug ¶
func ResponseFallbackDebug(debug bool) ResponseFallbackOption
ResponseFallbackDebug additional debug information.
func ResponseFallbackLogger ¶
func ResponseFallbackLogger(l utils.Logger) ResponseFallbackOption
ResponseFallbackLogger defines the logger used by ResponseFallback.
type WebhookSideEffect ¶
type WebhookSideEffect struct {
// contains filtered or unexported fields
}
WebhookSideEffect a web hook side effect.
func NewWebhookSideEffect ¶
func NewWebhookSideEffect(w Webhook) (*WebhookSideEffect, error)
NewWebhookSideEffect creates a new WebhookSideEffect.
func NewWebhookSideEffectsWithLogger ¶
func NewWebhookSideEffectsWithLogger(w Webhook, l utils.Logger) (*WebhookSideEffect, error)
NewWebhookSideEffectsWithLogger creates a new WebhookSideEffect.
func (*WebhookSideEffect) Exec ¶
func (w *WebhookSideEffect) Exec() error
Exec execute the side effect.