diff --git a/.vscode/settings.json b/.vscode/settings.json index ae4a956..0028a32 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { + "window.autoDetectColorScheme": false, "editor.tabSize": 2, "editor.semanticTokenColorCustomizations":{ "enabled": true, // enable semantic highlighting for all themes @@ -10,6 +11,7 @@ "parameter":"#4c70e7", } }, + "extensions.showRecommendationsOnlyOnDemand": true, "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true, "extensions.ignoreRecommendations": true, @@ -21,15 +23,15 @@ "activityBar.inactiveForeground": "#15202b99", "activityBarBadge.background": "#cfd3ef", "activityBarBadge.foreground": "#15202b", - "titleBar.activeBackground": "#5e7959", - "titleBar.inactiveBackground": "#5e795999", - "titleBar.activeForeground": "#e7e7e7", - "titleBar.inactiveForeground": "#e7e7e799", "statusBarItem.hoverBackground": "#759570", "statusBar.foreground": "#e7e7e7", "activityBar.activeBackground": "#759570", "statusBar.background": "#5e7959", "statusBar.border": "#5e7959", + "titleBar.activeBackground": "#5e7959", + "titleBar.inactiveBackground": "#5e795999", + "titleBar.activeForeground": "#e7e7e7", + "titleBar.inactiveForeground": "#e7e7e799", "titleBar.border": "#5e7959" }, "go.useLanguageServer": true, diff --git a/README.md b/README.md index 4023528..8110a26 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,8 @@ go test ./... ## 弹性模式 Resiliency Patterns -+ [ ] [WIP][熔断模式(circuit breaker](./resiliency/06_circuit_breaker) -+ [x] [限流模式(rate limiting))](./resiliency/07_rate_limiting) ++ [x] [熔断模式(circuit breaker)](./resiliency/06_circuit_breaker) ++ [x] [限流模式(rate limiting)](./resiliency/07_rate_limiting) + [ ] [WIP][重试模式(retrier)](./resiliency/15_retrier) + [ ] [WIP][最后期限模式(deadline)](./resiliency/14_deadline) diff --git a/resiliency/06_circuit_breaker/circuit_breaker_adv.go b/resiliency/06_circuit_breaker/circuit_breaker_adv.go index 15e9acd..ed01163 100644 --- a/resiliency/06_circuit_breaker/circuit_breaker_adv.go +++ b/resiliency/06_circuit_breaker/circuit_breaker_adv.go @@ -82,33 +82,32 @@ func (rb *RequestBreaker) Do(work func(ctx context.Context) (interface{}, error) //before fmt.Println("before do : request:", rb.cnter.Total()) - rb.mutex.Lock() - //handle status of Open to HalfOpen - if rb.state == StateOpen && rb.options.Expiry.Before(time.Now()) { - rb.state = StateHalfOpen - rb.cnter.Reset() - rb.options.OnStateChanged(rb.options.Name, StateOpen, StateHalfOpen) - } - rb.mutex.Unlock() - switch rb.state { case StateOpen: return nil, ErrTooManyRequests case StateHalfOpen: //do work from requested user - result, err := work(rb.options.Ctx) - if err != nil { - rb.cnter.Count(FailureState) + // result, err := work(rb.options.Ctx) + // if err != nil { + // rb.cnter.Count(FailureState) + // } else { + // rb.cnter.Count(SuccessState) + // return result, nil + // } - } else { - rb.cnter.Count(SuccessState) - return result, nil + if rb.options.Expiry.Before(time.Now()) { + rb.state = StateHalfOpen + rb.cnter.Reset() + rb.options.OnStateChanged(rb.options.Name, StateOpen, StateHalfOpen) } case StateClosed: } + rb.mutex.Unlock() + + //do work //do work from requested user result, err := work(rb.options.Ctx)