Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Breaker ¶
type Breaker interface { Succeed() Fail() FailWithTrip(TripFunc) Timeout() TimeoutWithTrip(TripFunc) IsAllowed() bool State() State Metricer() Metricer Reset() }
Breaker is the base of a circuit breaker.
type BreakerStateChangeHandler ¶
BreakerStateChangeHandler .
type Metricer ¶
type Metricer interface { Failures() int64 // return the number of failures Successes() int64 // return the number of successes Timeouts() int64 // return the number of timeouts ConseErrors() int64 // return the consecutive errors recently ConseTime() time.Duration // return the consecutive error time ErrorRate() float64 // rate = (timeouts + failures) / (timeouts + failures + successes) Samples() int64 // (timeouts + failures + successes) Counts() (successes, failures, timeouts int64) }
Metricer metrics errors, timeouts and successes
type Options ¶
type Options struct { // parameters for metricser BucketTime time.Duration // the time each bucket holds BucketNums int32 // the number of buckets the breaker have // parameters for breaker CoolingTimeout time.Duration // fixed when create DetectTimeout time.Duration // fixed when create HalfOpenSuccesses int32 // halfopen success is the threshold when the breaker is in HalfOpen; ShouldTrip TripFunc // can be nil ShouldTripWithKey TripFuncWithKey // can be nil, overwrites ShouldTrip BreakerStateChangeHandler BreakerStateChangeHandler // can be nil // if to use Per-P Metricer // use Per-P Metricer can increase performance in multi-P condition // but will consume more memory EnableShardP bool // Default value is time.Now, caller may use some high-performance custom time now func here Now func() time.Time }
Options for breaker
type Panel ¶
type Panel interface { Succeed(key string) Fail(key string) FailWithTrip(key string, f TripFunc) Timeout(key string) TimeoutWithTrip(key string, f TripFunc) IsAllowed(key string) bool RemoveBreaker(key string) DumpBreakers() map[string]Breaker // Close should be called if Panel is not used anymore. Or may lead to resource leak. // If Panel is used after Close is called, behavior is undefined. Close() GetMetricer(key string) Metricer }
Panel is what users should use.
type PanelStateChangeHandler ¶
PanelStateChangeHandler .
type State ¶
type State int32
State changes between Closed, Open, HalfOpen Closed -->- tripped ----> [Open]<-------+
^ | ^ | v | + | detect fail | | | | cooling timeout | ^ | ^ | v | +--- detect succeed --<-[HalfOpen]-->--+
The behaviors of each states: ================================================================================================= | | [Succeed] | [Fail or Timeout] | [IsAllowed] | |================================================================================================ | Closed | do nothing | if tripped, become Open | allow | |================================================================================================ | Open | do nothing | do nothing | if cooling timeout, allow; | | | | | else reject | |================================================================================================ | |increase halfopenSuccess, | | if detect timeout, allow; | |[HalfOpen] |if(halfopenSuccess >= | become Open | else reject | | | defaultHalfOpenSuccesses)| | | | | become Closed | | | =================================================================================================
type TripFunc ¶
TripFunc is a function called by a breaker when error appear and determines whether the breaker should trip.
func ConsecutiveTripFuncV2 ¶
func ConsecutiveTripFuncV2(rate float64, minSamples int64, duration time.Duration, durationSamples, conseErrors int64) TripFunc
ConsecutiveTripFuncV2 uses the following three strategies based on the parameters passed in. 1. when the number of samples >= minSamples and the error rate >= rate 2. when the number of samples >= durationSamples and the length of consecutive errors >= duration 3. When the number of consecutive errors >= conseErrors The fuse is opened when any of the above three strategies holds.
type TripFuncWithKey ¶
TripFuncWithKey returns a TripFunc according to the key.