breaker

package
v0.0.0-...-0985de9 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpenState      = errors.New("circuit breaker is open")
	ErrTooManyRequest = errors.New("too many requests")
)

Functions

func Do

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

Do calls Breaker.Do on the Breaker with given name. Do("any" , func () {})

func DoWithAcceptable

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

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

func DoWithFallback

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

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

func DoWithFallbackAcceptable

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

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

func Opt

func Opt(name string, opts ...Option)

Types

type Acceptable

type Acceptable func(err error) bool

type Breaker

type Breaker interface {
	Name() string

	Counts() Counts

	State() State

	Do(req func() error) error

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

	DoWithFallback(req func() error, fallback func(err error) error) error

	DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error
}

func GetBreaker

func GetBreaker(name string) Breaker

GetBreaker returns the Breaker with the given name.

type CircuitBreaker

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

func NewBreaker

func NewBreaker(opts ...Option) *CircuitBreaker

创建熔断器

func (*CircuitBreaker) Counts

func (cb *CircuitBreaker) Counts() Counts

func (*CircuitBreaker) Do

func (cb *CircuitBreaker) Do(req func() error) error

func (*CircuitBreaker) DoWithAcceptable

func (cb *CircuitBreaker) DoWithAcceptable(req func() error, acceptable Acceptable) error

func (*CircuitBreaker) DoWithFallback

func (cb *CircuitBreaker) DoWithFallback(req func() error, fallback func(err error) error) error

func (*CircuitBreaker) DoWithFallbackAcceptable

func (cb *CircuitBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error

func (*CircuitBreaker) Name

func (cb *CircuitBreaker) Name() string

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

type Counts

type Counts struct {
	// 总请求量
	Requests uint32
	// 总成功量
	TotalSuccesses uint32
	// 总失败量
	TotalFailures uint32
	// 连续成功量
	ConsecutiveSuccesses uint32
	// 连续失败量
	ConsecutiveFailures uint32
}

type Option

type Option func(opt *option)

func WithMaxRequest

func WithMaxRequest(maxRequest uint32) Option

func WithName

func WithName(name string) Option

func WithOnSstateChange

func WithOnSstateChange(onSstateChange func(name string, from State, to State)) Option

func WithReadyToTrip

func WithReadyToTrip(readyToTrip func(counts Counts) bool) Option

func WithTImeout

func WithTImeout(timeout time.Duration) Option

type State

type State int
const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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