diff --git a/core/breaker/breaker.go b/core/breaker/breaker.go index 18f32737..7cabbe2f 100644 --- a/core/breaker/breaker.go +++ b/core/breaker/breaker.go @@ -18,12 +18,14 @@ const ( timeFormat = "15:04:05" ) -// ErrServiceUnavailable is returned when the CB state is open +// ErrServiceUnavailable is returned when the Breaker state is open. var ErrServiceUnavailable = errors.New("circuit breaker is open") type ( + // Acceptable is the func to check if the error can be accepted. Acceptable func(err error) bool + // A Breaker represents a circuit breaker. Breaker interface { // Name returns the name of the Breaker. Name() string @@ -61,10 +63,14 @@ type ( DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error } + // Option defines the method to customize a Breaker. Option func(breaker *circuitBreaker) + // Promise interface defines the callbacks that returned by Breaker.Allow. Promise interface { + // Accept tells the Breaker that the call is successful. Accept() + // Reject tells the Breaker that the call is failed. Reject(reason string) } @@ -89,6 +95,8 @@ type ( } ) +// NewBreaker returns a Breaker object. +// opts can be used to customize the Breaker. func NewBreaker(opts ...Option) Breaker { var b circuitBreaker for _, opt := range opts { @@ -127,6 +135,7 @@ func (cb *circuitBreaker) Name() string { return cb.name } +// WithName returns a function to set the name of a Breaker. func WithName(name string) Option { return func(b *circuitBreaker) { b.name = name diff --git a/core/breaker/breakers.go b/core/breaker/breakers.go index f13b3e76..4adf189a 100644 --- a/core/breaker/breakers.go +++ b/core/breaker/breakers.go @@ -7,24 +7,28 @@ var ( breakers = make(map[string]Breaker) ) +// Do calls Breaker.Do on the Breaker with given name. func Do(name string, req func() error) error { return do(name, func(b Breaker) error { return b.Do(req) }) } +// DoWithAcceptable calls Breaker.DoWithAcceptable on the Breaker with given name. func DoWithAcceptable(name string, req func() error, acceptable Acceptable) error { return do(name, func(b Breaker) error { return b.DoWithAcceptable(req, acceptable) }) } +// DoWithFallback calls Breaker.DoWithFallback on the Breaker with given name. func DoWithFallback(name string, req func() error, fallback func(err error) error) error { return do(name, func(b Breaker) error { return b.DoWithFallback(req, fallback) }) } +// DoWithFallbackAcceptable calls Breaker.DoWithFallbackAcceptable on the Breaker with given name. func DoWithFallbackAcceptable(name string, req func() error, fallback func(err error) error, acceptable Acceptable) error { return do(name, func(b Breaker) error { @@ -32,6 +36,7 @@ func DoWithFallbackAcceptable(name string, req func() error, fallback func(err e }) } +// GetBreaker returns the Breaker with the given name. func GetBreaker(name string) Breaker { lock.RLock() b, ok := breakers[name] @@ -51,7 +56,8 @@ func GetBreaker(name string) Breaker { return b } -func NoBreakFor(name string) { +// NoBreakerFor disables the circuit breaker for the given name. +func NoBreakerFor(name string) { lock.Lock() breakers[name] = newNoOpBreaker() lock.Unlock() diff --git a/core/breaker/breakers_test.go b/core/breaker/breakers_test.go index 3a302517..1ffa2eca 100644 --- a/core/breaker/breakers_test.go +++ b/core/breaker/breakers_test.go @@ -55,7 +55,7 @@ func TestBreakersDoWithAcceptable(t *testing.T) { } func TestBreakersNoBreakerFor(t *testing.T) { - NoBreakFor("any") + NoBreakerFor("any") errDummy := errors.New("any") for i := 0; i < 10000; i++ { assert.Equal(t, errDummy, GetBreaker("any").Do(func() error {