circuitbreaker

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2014 License: Apache-2.0, Apache-2.0 Imports: 19 Imported by: 0

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

func MustParseExpression(in string) threshold.Predicate

MustParseExpresison calls ParseExpression and panics if expression is incorrect, for use in tests

func ParseExpression

func ParseExpression(in string) (threshold.Predicate, error)

ParseExpression parses expression in the go language into predicates.

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

func (c *CircuitBreaker) ProcessRequest(r request.Request) (*http.Response, error)

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 Redirect

type Redirect struct {
	URL string
}

type RedirectFallback

type RedirectFallback struct {
	// contains filtered or unexported fields
}

func NewRedirectFallback

func NewRedirectFallback(r Redirect) (*RedirectFallback, error)

func (*RedirectFallback) ProcessRequest

func (f *RedirectFallback) ProcessRequest(r request.Request) (*http.Response, error)

func (*RedirectFallback) ProcessResponse

func (f *RedirectFallback) ProcessResponse(r request.Request, a request.Attempt)

type Response

type Response struct {
	StatusCode  int
	ContentType string
	Body        []byte
}

type ResponseFallback

type ResponseFallback struct {
	// contains filtered or unexported fields
}

func NewResponseFallback

func NewResponseFallback(r Response) (*ResponseFallback, error)

func (*ResponseFallback) ProcessRequest

func (f *ResponseFallback) ProcessRequest(r request.Request) (*http.Response, error)

func (*ResponseFallback) ProcessResponse

func (f *ResponseFallback) ProcessResponse(r request.Request, a request.Attempt)

type SideEffect

type SideEffect interface {
	Exec() error
}

type Webhook

type Webhook struct {
	URL     string
	Method  string
	Headers http.Header
	Form    url.Values
	Body    []byte
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL