From 278efd15246b9cf648b95f60a59d29905f1b2d27 Mon Sep 17 00:00:00 2001 From: Edward Date: Wed, 29 Apr 2020 22:29:00 +0800 Subject: [PATCH] fix error and make up codes for circuit breakers patters --- README.md | 3 +- ...{profiles_test.go => time_profile_test.go} | 13 +++--- .../circuit_breaker/circuit_breaker_test.go | 42 +++++++++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) rename gomore/28_profiles/{profiles_test.go => time_profile_test.go} (84%) diff --git a/README.md b/README.md index 9ace33c..22716f7 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ Go常用的、面向工程化和最佳实践的模式套路,包含常见的23 [菜鸟教程—设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html) -[GO设计模式](https://github.com/senghoo/golang-design-pattern) +[23-Pattern-in-Go](https://github.com/senghoo/golang-design-pattern) + ## 更多 diff --git a/gomore/28_profiles/profiles_test.go b/gomore/28_profiles/time_profile_test.go similarity index 84% rename from gomore/28_profiles/profiles_test.go rename to gomore/28_profiles/time_profile_test.go index cd7f484..08a4de9 100644 --- a/gomore/28_profiles/profiles_test.go +++ b/gomore/28_profiles/time_profile_test.go @@ -1,27 +1,24 @@ -package profile +package timeprofile import ( "log" "math/big" - "time" "testing" + "time" ) - func TestBigintProfile(t *testing.T) { + bigint := big.NewInt(14468) - bigint := big.NewInt(14468) - - BigIntFactorial(bigint) + BigIntFactorial(bigint) bigint = big.NewInt(24566) - BigIntFactorial(bigint) + BigIntFactorial(bigint) } - //Duration for time differ func Duration(invocation time.Time, name string) { elapsed := time.Since(invocation) diff --git a/gomore/circuit_breaker/circuit_breaker_test.go b/gomore/circuit_breaker/circuit_breaker_test.go index 355c9e4..9a53c5b 100644 --- a/gomore/circuit_breaker/circuit_breaker_test.go +++ b/gomore/circuit_breaker/circuit_breaker_test.go @@ -2,9 +2,14 @@ package circuit import ( "context" + "errors" "time" ) +var ( + ErrServiceUnavailable = errors.New("Service Unavailable") +) + type State int const ( @@ -13,6 +18,7 @@ const ( SuccessState ) +//Counter interface type Counter interface { Count(State) ConsecutiveFailures() uint32 @@ -20,19 +26,47 @@ type Counter interface { Reset() } +type counters struct { + state State + lastActivity time.Time +} + +func (c *counters) Count(State) { + +} + +func (c *counters) ConsecutiveFailures() uint32 { + + return 0 +} + +func (c *counters) LastActivity() time.Time { + return c.lastActivity +} + +func (c *counters) Reset() { + +} + +func NewCounter() Counter { + var i Counter + return i +} + type Circuit func(context.Context) error func Breaker(c Circuit, failureThreshold uint32) Circuit { + cnt := NewCounter() - return func(ctx context) error { + return func(ctx context.Context) error { if cnt.ConsecutiveFailures() >= failureThreshold { - canRetry := func(cnt Counter) { - backoffLevel := Cnt.ConsecutiveFailures() - failureThreshold + canRetry := func(cnt Counter) bool { + backoffLevel := cnt.ConsecutiveFailures() - failureThreshold // Calculates when should the circuit breaker resume propagating requests // to the service - shouldRetryAt := cnt.LastActivity().Add(time.Seconds * 2 << backoffLevel) + shouldRetryAt := cnt.LastActivity().Add(time.Second * 2 << backoffLevel) return time.Now().After(shouldRetryAt) }