Documentation ¶
Overview ¶
Package circuitbreak implements the circuit breaker logic.
Index ¶
- Constants
- Variables
- func CopyDefaultCBConfig() iface.ConfigValueItem
- func NewCircuitBreakerMW(control Control, panel circuitbreaker.Panel) endpoint.Middleware
- func NoDecoration(ctx context.Context, request interface{}, err error) error
- func RPCInfo2Key(ri rpcinfo.RPCInfo) string
- func RecordStat(ctx context.Context, request, response interface{}, err error, cbKey string, ...)
- type CBConfig
- type CBConfigItem
- type CBSuite
- func (s *CBSuite) Close() error
- func (s *CBSuite) Dump() interface{}
- func (s *CBSuite) InstanceCBMW() endpoint.Middleware
- func (s *CBSuite) ServiceCBMW() endpoint.Middleware
- func (s *CBSuite) ServiceControl() *Control
- func (s *CBSuite) ServicePanel() circuitbreaker.Panel
- func (s *CBSuite) SetEventBusAndQueue(bus event.Bus, events event.Queue)
- func (s *CBSuite) UpdateInstanceCBConfig(cfg CBConfig)
- func (s *CBSuite) UpdateServiceCBConfig(key string, cfg CBConfig)
- type CBSuiteConfig
- type CBSuiteOption
- type CircuitBreakerAwareError
- type Control
- type ErrorType
- type GenServiceCBKeyFunc
- type GetErrorTypeFunc
- type Parameter
Constants ¶
const TypeCircuitBreaker iface.ItemType = "cb_config"
TypeCircuitBreaker is used as itemKey in ConfigValueImpl
Variables ¶
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 ¶
NoDecoration returns the original err.
func RPCInfo2Key ¶
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.
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, options ...CBSuiteOption) *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) 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 ¶
ServiceControl return cb Control of service
func (*CBSuite) ServicePanel ¶
func (s *CBSuite) ServicePanel() circuitbreaker.Panel
ServicePanel return cb Panel of service
func (*CBSuite) SetEventBusAndQueue ¶
SetEventBusAndQueue is to make CircuitBreaker relate to event change.
func (*CBSuite) UpdateInstanceCBConfig ¶
UpdateInstanceCBConfig is to update instance CircuitBreaker param. This func is suggested to be called in remote config module.
func (*CBSuite) UpdateServiceCBConfig ¶
UpdateServiceCBConfig is to update service CircuitBreaker config. This func is suggested to be called in remote config module.
type CBSuiteConfig ¶ added in v0.10.0
type CBSuiteConfig struct {
// contains filtered or unexported fields
}
type CBSuiteOption ¶ added in v0.10.0
type CBSuiteOption func(s *CBSuiteConfig)
func WithInstanceGetErrorType ¶ added in v0.10.0
func WithInstanceGetErrorType(f GetErrorTypeFunc) CBSuiteOption
WithInstanceGetErrorType sets instanceControl.GetErrorType Kitex will call customFunc to determine the error type for circuit breaker Users are recommended to use WithWrappedInstanceGetErrorType to keep most of the behaviors NOTE: this is used for INSTANCE LEVEL circuit breaker
func WithServiceGetErrorType ¶ added in v0.10.0
func WithServiceGetErrorType(customFunc GetErrorTypeFunc) CBSuiteOption
WithServiceGetErrorType sets serviceControl.GetErrorType Kitex will call customFunc to determine the error type for circuit breaker Users are recommended to use WithWrappedServiceGetErrorType to keep most of the behaviors NOTE: this is used for SERVICE LEVEL circuit breaker
func WithWrappedInstanceGetErrorType ¶ added in v0.10.0
func WithWrappedInstanceGetErrorType(f GetErrorTypeFunc) CBSuiteOption
WithWrappedInstanceGetErrorType sets instanceControl.GetErrorType Kitex will call ErrorTypeOnInstanceLevel first, and if TypeSuccess is returned, customFunc will then be called to determine the final error type for circuit breaker NOTE: this is used for INSTANCE LEVEL circuit breaker
func WithWrappedServiceGetErrorType ¶ added in v0.10.0
func WithWrappedServiceGetErrorType(customFunc GetErrorTypeFunc) CBSuiteOption
WithWrappedServiceGetErrorType sets serviceControl.GetErrorType Kitex will call ErrorTypeOnServiceLevel first, and if TypeSuccess is returned, customFunc will then be called to determine the final error type for circuit breaker NOTE: this is used for SERVICE LEVEL circuit breaker
type CircuitBreakerAwareError ¶ added in v0.0.4
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 GetErrorTypeFunc // 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.
type GenServiceCBKeyFunc ¶
GenServiceCBKeyFunc to generate circuit breaker key through rpcinfo. You can customize the config key according to your config center.
type GetErrorTypeFunc ¶ added in v0.10.0
func WrapErrorTypeFunc ¶ added in v0.10.0
func WrapErrorTypeFunc(customFunc, originalFunc GetErrorTypeFunc) GetErrorTypeFunc
WrapErrorTypeFunc calls the customFunc if the originalFunc returns TypeSuccess customFunc may selectively return another type based on business requirement
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.