oxy: github.com/vulcand/oxy/cbreaker Index | Files

package cbreaker

import "github.com/vulcand/oxy/cbreaker"

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

Package Files

cbreaker.go effect.go fallback.go predicates.go ratio.go

type CircuitBreaker Uses

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

CircuitBreaker is http.Handler that implements circuit breaker pattern

func New Uses

func New(next http.Handler, expression string, options ...CircuitBreakerOption) (*CircuitBreaker, error)

New creates a new CircuitBreaker middleware

func (*CircuitBreaker) Fallback Uses

func (c *CircuitBreaker) Fallback(f http.Handler)

Fallback sets the fallback handler to be called by circuit breaker handler.

func (*CircuitBreaker) ServeHTTP Uses

func (c *CircuitBreaker) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*CircuitBreaker) String Uses

func (c *CircuitBreaker) String() string

String returns log-friendly representation of the circuit breaker state

func (*CircuitBreaker) Wrap Uses

func (c *CircuitBreaker) Wrap(next http.Handler)

Wrap sets the next handler to be called by circuit breaker handler.

type CircuitBreakerOption Uses

type CircuitBreakerOption func(*CircuitBreaker) error

CircuitBreakerOption represents an option you can pass to New. See the documentation for the individual options below.

func CheckPeriod Uses

func CheckPeriod(d time.Duration) CircuitBreakerOption

CheckPeriod is how long the CircuitBreaker will wait between successive checks of the breaker condition.

func Clock Uses

func Clock(clock timetools.TimeProvider) CircuitBreakerOption

Clock allows you to fake che CircuitBreaker's view of the current time. Intended for unit tests.

func Fallback Uses

func Fallback(h http.Handler) CircuitBreakerOption

Fallback defines the http.Handler that the CircuitBreaker should route requests to when it prevents a request from taking its normal path.

func FallbackDuration Uses

func FallbackDuration(d time.Duration) CircuitBreakerOption

FallbackDuration is how long the CircuitBreaker will remain in the Tripped state before trying to recover.

func Logger Uses

func Logger(l *log.Logger) CircuitBreakerOption

Logger defines the logger the circuit breaker will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus.

func OnStandby Uses

func OnStandby(s SideEffect) CircuitBreakerOption

OnStandby sets a SideEffect to run when entering the Standby state. Only one SideEffect can be set for this hook.

func OnTripped Uses

func OnTripped(s SideEffect) CircuitBreakerOption

OnTripped sets a SideEffect to run when entering the Tripped state. Only one SideEffect can be set for this hook.

func RecoveryDuration Uses

func RecoveryDuration(d time.Duration) CircuitBreakerOption

RecoveryDuration is how long the CircuitBreaker will take to ramp up requests during the Recovering state.

type Redirect Uses

type Redirect struct {
    URL          string
    PreservePath bool
}

Redirect redirect model

type RedirectFallback Uses

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

RedirectFallback fallback redirect handler

func NewRedirectFallback Uses

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

NewRedirectFallback creates a new RedirectFallback

func NewRedirectFallbackWithLogger Uses

func NewRedirectFallbackWithLogger(r Redirect, l *log.Logger) (*RedirectFallback, error)

NewRedirectFallbackWithLogger creates a new RedirectFallback

func (*RedirectFallback) ServeHTTP Uses

func (f *RedirectFallback) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Response Uses

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

Response response model

type ResponseFallback Uses

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

ResponseFallback fallback response handler

func NewResponseFallback Uses

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

NewResponseFallback creates a new ResponseFallback

func NewResponseFallbackWithLogger Uses

func NewResponseFallbackWithLogger(r Response, l *log.Logger) (*ResponseFallback, error)

NewResponseFallbackWithLogger creates a new ResponseFallback

func (*ResponseFallback) ServeHTTP Uses

func (f *ResponseFallback) ServeHTTP(w http.ResponseWriter, req *http.Request)

type SideEffect Uses

type SideEffect interface {
    Exec() error
}

SideEffect a side effect

type Webhook Uses

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

Webhook Web hook

type WebhookSideEffect Uses

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

WebhookSideEffect a web hook side effect

func NewWebhookSideEffect Uses

func NewWebhookSideEffect(w Webhook) (*WebhookSideEffect, error)

NewWebhookSideEffect creates a new WebhookSideEffect

func NewWebhookSideEffectsWithLogger Uses

func NewWebhookSideEffectsWithLogger(w Webhook, l *log.Logger) (*WebhookSideEffect, error)

NewWebhookSideEffectsWithLogger creates a new WebhookSideEffect

func (*WebhookSideEffect) Exec Uses

func (w *WebhookSideEffect) Exec() error

Exec execute the side effect

Package cbreaker imports 15 packages (graph) and is imported by 74 packages. Updated 2020-08-02. Refresh now. Tools for package owners.