diff --git a/gomore/06_circuit_breaker/breaker_options.go b/gomore/06_circuit_breaker/breaker_options.go index 9ecf554..e03f786 100644 --- a/gomore/06_circuit_breaker/breaker_options.go +++ b/gomore/06_circuit_breaker/breaker_options.go @@ -4,13 +4,12 @@ import "time" //Options for breaker type Options struct { - Name string - Expiry time.Time - Interval time.Duration - Timeout time.Duration - MaxRequests uint32 - ReadyToTrip StateCheckerHandler - OnStateChanged StateChangedEventHandler + Name string + Expiry time.Time + Interval, Timeout time.Duration + MaxRequests uint32 + ReadyToTrip StateCheckerHandler + OnStateChanged StateChangedEventHandler } //SetName of breaker diff --git a/gomore/06_circuit_breaker/circuit_breaker.go b/gomore/06_circuit_breaker/circuit_breaker.go index 182425f..afee9c7 100644 --- a/gomore/06_circuit_breaker/circuit_breaker.go +++ b/gomore/06_circuit_breaker/circuit_breaker.go @@ -5,7 +5,7 @@ package circuit * @Author: Edward * @Date: 2020-05-10 22:00:58 * @Last Modified by: Edward - * @Last Modified time: 2020-05-10 22:02:18 + * @Last Modified time: 2020-05-11 11:57:21 */ import ( @@ -46,8 +46,28 @@ type RequestBreaker struct { } //NewRequestBreaker return a breaker -func NewRequestBreaker() *RequestBreaker { +func NewRequestBreaker(opts ...Option) *RequestBreaker { + defaultOptions := Options{ + Name: "defaultBreakerName", + Expiry: time.Now().Add(time.Second * 20), + Interval: time.Second * 2, + Timeout: time.Second * 5, + MaxRequests: 5, + ReadyToTrip: func(counts counters) bool { return true }, + OnStateChanged: func(name string, from State, to State) {}, + } + + for _, setOption := range opts { + setOption(&defaultOptions) + + } + + return &RequestBreaker{ + options: defaultOptions, + counts: nil, + generation: 0, + } } //State of current switch diff --git a/gomore/06_circuit_breaker/circuit_breaker_test.go b/gomore/06_circuit_breaker/circuit_breaker_test.go index e0f6daa..0fede6c 100644 --- a/gomore/06_circuit_breaker/circuit_breaker_test.go +++ b/gomore/06_circuit_breaker/circuit_breaker_test.go @@ -1,3 +1,11 @@ +/* + * @Description: https://github.com/crazybber + * @Author: Edward + * @Date: 2020-05-11 10:55:28 + * @Last Modified by: Edward + * @Last Modified time: 2020-05-11 10:55:28 + */ + package circuit import ( @@ -14,6 +22,7 @@ func TestBasicBreaker(t *testing.T) { var st Options st.Name = "HTTP GET" st.ReadyToTrip = func(counts counters) bool { + //失败率,可以由用户自己定义 failureRatio := float64(counts.TotalFailures) / float64(counts.Requests) return counts.Requests >= 3 && failureRatio >= 0.6 } diff --git a/gomore/09_parallelism/parallelism_test.go b/gomore/09_parallelism/parallelism_test.go index 90b3590..f6e5da5 100644 --- a/gomore/09_parallelism/parallelism_test.go +++ b/gomore/09_parallelism/parallelism_test.go @@ -1,3 +1,11 @@ +/* + * @Description: https://github.com/crazybber + * @Author: Edward + * @Date: 2020-05-11 10:44:52 + * @Last Modified by: Edward + * @Last Modified time: 2020-05-11 10:44:52 + */ + package parallelism import (