Documentation ¶
Overview ¶
Package circuitbreak implements the circuit breaker logic.
Index ¶
- 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 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 Control
- type ErrorType
- type GenServiceCBKeyFunc
- type Parameter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
func GetDefaultCBConfig ¶
func GetDefaultCBConfig() CBConfig
GetDefaultCBConfig return defaultConfig of CircuitBreaker.
type CBSuite ¶
type CBSuite struct {
// contains filtered or unexported fields
}
CBSuite is default wrapper of CircuitBreaker. If you don't have customize policy, you can specify CircuitBreaker middlewares like this:
cbs := NewCBSuite(GenServiceCBKeyFunc) opts = append(opts, client.WithMiddleware(cbs.ServiceCBMW())) opts = append(opts, client.WithInstanceMW(cbs.InstanceCBMW()))
Notice: when you build middlewares, ServiceCBMW is suggested to be the first mw and set before timeout mw especially.
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) 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 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 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 a 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 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.