mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2025-02-19 23:23:14 +03:00
Update circuit-breaker.md
This commit is contained in:
parent
5b0c884618
commit
c27fe6fd88
@ -64,36 +64,43 @@ type Circuit func(context.Context) error
|
|||||||
|
|
||||||
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
||||||
cnt := NewCounter()
|
cnt := NewCounter()
|
||||||
|
expired := time.Now()
|
||||||
|
currentState := StateClosed
|
||||||
|
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
|
||||||
return func(ctx context) error {
|
|
||||||
if cnt.ConsecutiveFailures() >= failureThreshold {
|
|
||||||
canRetry := func(cnt Counter) {
|
|
||||||
backOffLevel := Cnt.ConsecutiveFailures() - failureThreshold
|
|
||||||
|
|
||||||
// Calculates when should the circuit breaker resume propagating requests
|
//handle statue transformation for timeout
|
||||||
// to the service
|
if currentState == StateOpen {
|
||||||
shouldRetryAt := cnt.LastActivity().Add(time.Seconds * 2 << backOffLevel)
|
nowt := time.Now()
|
||||||
|
if expired.Before(nowt) || expired.Equal(nowt) {
|
||||||
|
currentState = StateHalfOpen
|
||||||
|
cnt.ConsecutiveSuccesses = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return time.Now().After(shouldRetryAt)
|
switch currentState {
|
||||||
}
|
case StateOpen:
|
||||||
|
return ErrServiceUnavailable
|
||||||
|
case StateHalfOpen:
|
||||||
|
if err := c(ctx); err != nil {
|
||||||
|
currentState = StateOpen
|
||||||
|
expired = time.Now().Add(defaultTimeout) //Reset
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cnt.Count(SuccessState)
|
||||||
|
if cnt.ConsecutiveSuccesses > defaultSuccessThreshold {
|
||||||
|
currentState = StateClosed
|
||||||
|
cnt.ConsecutiveFailures = 0
|
||||||
|
}
|
||||||
|
|
||||||
if !canRetry(cnt) {
|
case StateClosed:
|
||||||
// Fails fast instead of propagating requests to the circuit since
|
if err := c(ctx); err != nil {
|
||||||
// not enough time has passed since the last failure to retry
|
cnt.Count(FailureState)
|
||||||
return ErrServiceUnavailable
|
}
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
|
}
|
||||||
// Unless the failure threshold is exceeded the wrapped service mimics the
|
|
||||||
// old behavior and the difference in behavior is seen after consecutive failures
|
|
||||||
if err := c(ctx); err != nil {
|
|
||||||
cnt.Count(FailureState)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cnt.Count(SuccessState)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user