Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix success handling in the half-open and add flow control #1805

Merged
merged 8 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions internal/circuitbreaker/breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func newBreaker(key string, opts ...BreakerOption) (*breaker, error) {
// do executes the function given argument when the current breaker state is "Closed" or "Half-Open".
// If the current breaker state is "Open", this function returns ErrCircuitBreakerOpenState.
func (b *breaker) do(ctx context.Context, fn func(ctx context.Context) (val interface{}, err error)) (val interface{}, st State, err error) {
if !b.isReady() {
return nil, StateOpen, errors.ErrCircuitBreakerOpenState
if st, err := b.isReady(); err != nil {
return nil, st, err
}
val, err = fn(ctx)
if err != nil {
Expand Down Expand Up @@ -98,14 +98,32 @@ func (b *breaker) do(ctx context.Context, fn func(ctx context.Context) (val inte

// isReady determines the breaker is ready or not.
// If the current breaker state is "Closed" or "Half-Open", this function returns true.
func (b *breaker) isReady() (ok bool) {
st := b.currentState()
return st == StateClosed || st == StateHalfOpen
func (b *breaker) isReady() (st State, err error) {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
st = b.currentState()
switch st {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
case StateOpen:
return st, errors.ErrCircuitBreakerOpenState
case StateHalfOpen:

// For flow control in the "Half-Open" state. It is limited to 50%.
// If this modulo is used, 1/2 of the requests will be error. And if an error occurs, mark as failures.
cnt := b.count.Load().(*count)
total := cnt.Total()
if total%2 == 0 {
cnt.onFail()
return st, errors.ErrCircuitBreakerHalfOpenFlowLimitation
}
}
return st, nil
}

func (b *breaker) success() {
b.count.Load().(*count).onSuccess()
if st := b.currentState(); st == StateHalfOpen {
cnt := b.count.Load().(*count)
cnt.onSuccess()

// halfOpenErrShouldTrip.ShouldTrip returns true when the sum of the number of successes and failures is greater than the b.minSamples and when the error rate is greater than the b.halfOpenErrRate.
// In other words, if the error rate is less than the b.halfOpenErrRate, it can be judged that the success rate is high, so this function change to the "Close" state from "Half-Open".
if st := b.currentState(); st == StateHalfOpen && cnt.Total() >= b.minSamples && !b.halfOpenErrShouldTrip.ShouldTrip(cnt) {
log.Infof("the operation succeeded, circuit breaker state for '%s' changed,\tfrom: %s, to: %s", b.key, st.String(), StateClosed.String())
b.reset()
}
Expand Down
Loading