Merge remote-tracking branch 'origin/master'

This commit is contained in:
Edward 2020-06-02 16:10:17 +08:00
commit a0fc294790
3 changed files with 22 additions and 21 deletions

10
.vscode/settings.json vendored
View File

@ -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,

View File

@ -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)

View File

@ -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)