mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 14:26:04 +03:00
Update circuit-breaker.md
This commit is contained in:
parent
ac6bb97b58
commit
33e5b0d51a
@ -60,45 +60,52 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type State int
|
||||||
|
|
||||||
|
const (
|
||||||
|
UnknownState State = iota
|
||||||
|
FailureState
|
||||||
|
SuccessState
|
||||||
|
)
|
||||||
|
|
||||||
|
type Counter interface {
|
||||||
|
Count(State)
|
||||||
|
ConsecutiveFailures() uint32
|
||||||
|
LastActivity() time.Time
|
||||||
|
Reset()
|
||||||
|
}
|
||||||
|
|
||||||
type Circuit func(context.Context) error
|
type Circuit func(context.Context) error
|
||||||
|
|
||||||
|
var canRetry = func(cnt counters, failureThreshold uint32) bool {
|
||||||
|
backoffLevel := cnt.ConsecutiveFailures - failureThreshold
|
||||||
|
// Calculates when should the circuit breaker resume propagating requests
|
||||||
|
// to the service
|
||||||
|
shouldRetryAt := cnt.LastActivity().Add(time.Second << backoffLevel)
|
||||||
|
return time.Now().After(shouldRetryAt)
|
||||||
|
}
|
||||||
|
|
||||||
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
||||||
cnt := NewCounter()
|
cnt := counters{}
|
||||||
expired := time.Now()
|
|
||||||
currentState := StateClosed
|
|
||||||
|
|
||||||
return func(ctx context.Context) error {
|
//ctx can be used hold parameters
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
|
||||||
|
if cnt.ConsecutiveFailures >= failureThreshold {
|
||||||
//handle statue transformation for timeout
|
if !canRetry(cnt, failureThreshold) {
|
||||||
if currentState == StateOpen {
|
// Fails fast instead of propagating requests to the circuit since
|
||||||
nowt := time.Now()
|
// not enough time has passed since the last failure to retry
|
||||||
if expired.Before(nowt) || expired.Equal(nowt) {
|
return ErrServiceUnavailable
|
||||||
currentState = StateHalfOpen
|
|
||||||
cnt.ConsecutiveSuccesses = 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Unless the failure threshold is exceeded the wrapped service mimics the
|
||||||
switch currentState {
|
// old behavior and the difference in behavior is seen after consecutive failures
|
||||||
case StateOpen:
|
if err := c(ctx); err != nil {
|
||||||
return ErrServiceUnavailable
|
cnt.Count(FailureState)
|
||||||
case StateHalfOpen:
|
return err
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
case StateClosed:
|
|
||||||
if err := c(ctx); err != nil {
|
|
||||||
cnt.Count(FailureState)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cnt.Count(SuccessState)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user