circuitbreak

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 14 Imported by: 20

Documentation

Overview

Package circuitbreak implements the circuit breaker logic.

Index

Constants

View Source
const TypeCircuitBreaker iface.ItemType = "cb_config"

TypeCircuitBreaker is used as itemKey in ConfigValueImpl

Variables

View Source
var NewCBConfig = util.JsonInitializer(func() iface.ConfigValueItem {
	return &CBConfigItem{}
})

NewCBConfig decodes json bytes to a new CBConfigItem

Functions

func CopyDefaultCBConfig added in v0.6.0

func CopyDefaultCBConfig() iface.ConfigValueItem

CopyDefaultCBConfig returns a copy of default CBConfig, thus avoiding default values changed by business

func NewCircuitBreakerMW

func NewCircuitBreakerMW(control Control, panel circuitbreaker.Panel) endpoint.Middleware

NewCircuitBreakerMW creates a circuit breaker MW using the given Control strategy and Panel.

func NoDecoration

func NoDecoration(ctx context.Context, request interface{}, err error) error

NoDecoration returns the original err.

func RPCInfo2Key

func RPCInfo2Key(ri rpcinfo.RPCInfo) string

RPCInfo2Key is to generate circuit breaker key through rpcinfo

func RecordStat

func RecordStat(ctx context.Context, request, response interface{}, err error, cbKey string, ctl *Control, panel circuitbreaker.Panel)

RecordStat to report request result to circuit breaker

Types

type CBConfig

type CBConfig struct {
	Enable    bool    `json:"enable"`
	ErrRate   float64 `json:"err_rate"`
	MinSample int64   `json:"min_sample"`
}

CBConfig is policy config of CircuitBreaker. DON'T FORGET to update DeepCopy() and Equals() if you add new fields.

func GetDefaultCBConfig

func GetDefaultCBConfig() CBConfig

GetDefaultCBConfig return defaultConfig of CircuitBreaker.

func (*CBConfig) DeepCopy added in v0.5.0

func (c *CBConfig) DeepCopy() *CBConfig

DeepCopy returns a full copy of CBConfig.

func (*CBConfig) Equals added in v0.5.0

func (c *CBConfig) Equals(other *CBConfig) bool

type CBConfigItem added in v0.6.0

type CBConfigItem CBConfig

CBConfigItem is an alias of CBConfig to meet the requirement of iface.ConfigValueItem

func (*CBConfigItem) DeepCopy added in v0.6.0

func (c *CBConfigItem) DeepCopy() iface.ConfigValueItem

DeepCopy returns a copy of CBConfigItem

func (*CBConfigItem) EqualsTo added in v0.6.0

func (c *CBConfigItem) EqualsTo(other iface.ConfigValueItem) bool

EqualsTo compares two CBConfigItem

type CBSuite

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

CBSuite is default wrapper of CircuitBreaker. If you don't have customized policy, you can specify CircuitBreaker middlewares like this:

cbs := NewCBSuite(GenServiceCBKeyFunc)
opts = append(opts, client.WithCircuitBreaker(cbs))

func NewCBSuite

func NewCBSuite(genKey GenServiceCBKeyFunc) *CBSuite

NewCBSuite to build a new CBSuite. Notice: Should NewCBSuite for every client in this version, because event.Queue and event.Bus are not shared with all clients now.

func (*CBSuite) Close

func (s *CBSuite) Close() error

Close circuitbreaker.Panel to release associated resources.

func (*CBSuite) Dump

func (s *CBSuite) Dump() interface{}

Dump is to dump CircuitBreaker info for debug query.

func (*CBSuite) InstanceCBMW

func (s *CBSuite) InstanceCBMW() endpoint.Middleware

InstanceCBMW return a new instance level CircuitBreakerMW.

func (*CBSuite) ServiceCBMW

func (s *CBSuite) ServiceCBMW() endpoint.Middleware

ServiceCBMW return a new service level CircuitBreakerMW.

func (*CBSuite) ServiceControl

func (s *CBSuite) ServiceControl() *Control

ServiceControl return cb Control of service

func (*CBSuite) ServicePanel

func (s *CBSuite) ServicePanel() circuitbreaker.Panel

ServicePanel return return cb Panel of service

func (*CBSuite) SetEventBusAndQueue

func (s *CBSuite) SetEventBusAndQueue(bus event.Bus, events event.Queue)

SetEventBusAndQueue is to make CircuitBreaker relate to event change.

func (*CBSuite) UpdateInstanceCBConfig

func (s *CBSuite) UpdateInstanceCBConfig(cfg CBConfig)

UpdateInstanceCBConfig is to update instance CircuitBreaker param. This func is suggested to be called in remote config module.

func (*CBSuite) UpdateServiceCBConfig

func (s *CBSuite) UpdateServiceCBConfig(key string, cfg CBConfig)

UpdateServiceCBConfig is to update service CircuitBreaker config. This func is suggested to be called in remote config module.

type CircuitBreakerAwareError added in v0.0.4

type CircuitBreakerAwareError interface {
	error
	TypeForCircuitBreaker() ErrorType
}

CircuitBreakerAwareError is used to wrap ErrorType

func WrapErrorWithType added in v0.0.4

func WrapErrorWithType(err error, errorType ErrorType) CircuitBreakerAwareError

WrapErrorWithType is used to define the ErrorType for CircuitBreaker. If you don't want the error trigger fuse, you can set the ErrorType to TypeIgnorable, the error won't be regarded as failed. eg: return circuitbreak.WrapErrorWithType.WithCause(err, circuitbreak.TypeIgnorable) in customized middleware.

type Control

type Control struct {
	// Implement this to generate a key for the circuit breaker panel.
	GetKey func(ctx context.Context, request interface{}) (key string, enabled bool)

	// Implement this to determine the type of error.
	GetErrorType func(ctx context.Context, request, response interface{}, err error) ErrorType

	// Implement this to provide more detailed information about the circuit breaker.
	// The err argument is always a kerrors.ErrCircuitBreak.
	DecorateError func(ctx context.Context, request interface{}, err error) error
}

Control is the control strategy of the circuit breaker.

type ErrorType

type ErrorType int

ErrorType means the error type.

const (
	// TypeIgnorable means ignorable error, which is ignored by the circuit breaker.
	TypeIgnorable ErrorType = iota
	// TypeTimeout means timeout error.
	TypeTimeout
	// TypeFailure means the request failed, but it isn't timeout.
	TypeFailure
	// TypeSuccess means the request successes.
	TypeSuccess
)

Constants for ErrorType.

func ErrorTypeOnInstanceLevel

func ErrorTypeOnInstanceLevel(ctx context.Context, request, response interface{}, err error) ErrorType

ErrorTypeOnInstanceLevel determines the error type with an instance level criteria. Basically, it treats only the connection error as failure.

func ErrorTypeOnServiceLevel

func ErrorTypeOnServiceLevel(ctx context.Context, request, response interface{}, err error) ErrorType

ErrorTypeOnServiceLevel determines the error type with a service level criteria.

func FailIfError

func FailIfError(ctx context.Context, request, response interface{}, err error) ErrorType

FailIfError return TypeFailure if err is not nil, otherwise TypeSuccess.

type GenServiceCBKeyFunc

type GenServiceCBKeyFunc func(ri rpcinfo.RPCInfo) string

GenServiceCBKeyFunc to generate circuit breaker key through rpcinfo. You can customize the config key according to your config center.

type Parameter

type Parameter struct {
	// Enabled means if to enable the circuit breaker.
	Enabled bool
	// ErrorRate means the rate at which breaks.
	ErrorRate float64
	// MinimalSample means the minimal sample need before break.
	MinimalSample int64
}

Parameter contains parameters for circuit breaker.

Jump to

Keyboard shortcuts

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