breaker

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrServiceUnavailable = errors.New("circuit breaker is open")

ErrServiceUnavailable is returned when the Breaker state is open.

Functions

func Do

func Do(name string, req func() error) error

Do calls Breaker.Do on the Breaker with given name.

func DoCtx

func DoCtx(ctx context.Context, name string, req func() error) error

DoCtx calls Breaker.DoCtx on the Breaker with given name.

func DoWithAcceptable

func DoWithAcceptable(name string, req func() error, acceptable Acceptable) error

DoWithAcceptable calls Breaker.DoWithAcceptable on the Breaker with given name.

func DoWithAcceptableCtx

func DoWithAcceptableCtx(ctx context.Context, name string, req func() error,
	acceptable Acceptable) error

DoWithAcceptableCtx calls Breaker.DoWithAcceptableCtx on the Breaker with given name.

func DoWithFallback

func DoWithFallback(name string, req func() error, fallback Fallback) error

DoWithFallback calls Breaker.DoWithFallback on the Breaker with given name.

func DoWithFallbackAcceptable

func DoWithFallbackAcceptable(name string, req func() error, fallback Fallback,
	acceptable Acceptable) error

DoWithFallbackAcceptable calls Breaker.DoWithFallbackAcceptable on the Breaker with given name.

func DoWithFallbackAcceptableCtx

func DoWithFallbackAcceptableCtx(ctx context.Context, name string, req func() error,
	fallback Fallback, acceptable Acceptable) error

DoWithFallbackAcceptableCtx calls Breaker.DoWithFallbackAcceptableCtx on the Breaker with given name.

func DoWithFallbackCtx

func DoWithFallbackCtx(ctx context.Context, name string, req func() error, fallback Fallback) error

DoWithFallbackCtx calls Breaker.DoWithFallbackCtx on the Breaker with given name.

func NoBreakerFor

func NoBreakerFor(name string)

NoBreakerFor disables the circuit breaker for the given name.

Types

type Acceptable

type Acceptable func(err error) bool

Acceptable is the func to check if the error can be accepted.

type Breaker

type Breaker interface {
	// Name returns the name of the Breaker.
	Name() string

	// Allow checks if the request is allowed.
	// If allowed, a promise will be returned,
	// otherwise ErrServiceUnavailable will be returned as the error.
	// The caller needs to call promise.Accept() on success,
	// or call promise.Reject() on failure.
	Allow() (Promise, error)
	// AllowCtx checks if the request is allowed when ctx isn't done.
	AllowCtx(ctx context.Context) (Promise, error)

	// Do runs the given request if the Breaker accepts it.
	// Do returns an error instantly if the Breaker rejects the request.
	// If a panic occurs in the request, the Breaker handles it as an error
	// and causes the same panic again.
	Do(req func() error) error
	// DoCtx runs the given request if the Breaker accepts it when ctx isn't done.
	DoCtx(ctx context.Context, req func() error) error

	// DoWithAcceptable runs the given request if the Breaker accepts it.
	// DoWithAcceptable returns an error instantly if the Breaker rejects the request.
	// If a panic occurs in the request, the Breaker handles it as an error
	// and causes the same panic again.
	// acceptable checks if it's a successful call, even if the error is not nil.
	DoWithAcceptable(req func() error, acceptable Acceptable) error
	// DoWithAcceptableCtx runs the given request if the Breaker accepts it when ctx isn't done.
	DoWithAcceptableCtx(ctx context.Context, req func() error, acceptable Acceptable) error

	// DoWithFallback runs the given request if the Breaker accepts it.
	// DoWithFallback runs the fallback if the Breaker rejects the request.
	// If a panic occurs in the request, the Breaker handles it as an error
	// and causes the same panic again.
	DoWithFallback(req func() error, fallback Fallback) error
	// DoWithFallbackCtx runs the given request if the Breaker accepts it when ctx isn't done.
	DoWithFallbackCtx(ctx context.Context, req func() error, fallback Fallback) error

	// DoWithFallbackAcceptable runs the given request if the Breaker accepts it.
	// DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request.
	// If a panic occurs in the request, the Breaker handles it as an error
	// and causes the same panic again.
	// acceptable checks if it's a successful call, even if the error is not nil.
	DoWithFallbackAcceptable(req func() error, fallback Fallback, acceptable Acceptable) error
	// DoWithFallbackAcceptableCtx runs the given request if the Breaker accepts it when ctx isn't done.
	DoWithFallbackAcceptableCtx(ctx context.Context, req func() error, fallback Fallback,
		acceptable Acceptable) error
}

A Breaker represents a circuit breaker.

func GetBreaker

func GetBreaker(name string) Breaker

GetBreaker returns the Breaker with the given name.

func NewBreaker

func NewBreaker(opts ...Option) Breaker

NewBreaker returns a Breaker object. opts can be used to customize the Breaker.

func NopBreaker

func NopBreaker() Breaker

NopBreaker returns a breaker that never trigger breaker circuit.

type Fallback

type Fallback func(err error) error

Fallback is the func to be called if the request is rejected.

type Option

type Option func(breaker *circuitBreaker)

Option defines the method to customize a Breaker.

func WithName

func WithName(name string) Option

WithName returns a function to set the name of a Breaker.

type Promise

type Promise interface {
	// Accept tells the Breaker that the call is successful.
	Accept()
	// Reject tells the Breaker that the call is failed.
	Reject(reason string)
}

Promise interface defines the callbacks that returned by Breaker.Allow.

Jump to

Keyboard shortcuts

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