Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultOpenBackOff() backoff.BackOff
- func Ignore(err error) error
- func MarkAsSuccess(err error) error
- type BreakerOption
- func WithClock(clock clock.Clock) BreakerOption
- func WithCounterResetInterval(interval time.Duration) BreakerOption
- func WithFailOnContextCancel(failOnContextCancel bool) BreakerOption
- func WithFailOnContextDeadline(failOnContextDeadline bool) BreakerOption
- func WithHalfOpenMaxSuccesses(maxSuccesses int64) BreakerOption
- func WithOnStateChangeHookFn(hookFn StateChangeHook) BreakerOption
- func WithOpenTimeout(timeout time.Duration) BreakerOption
- func WithOpenTimeoutBackOff(backoff backoff.BackOff) BreakerOption
- func WithTripFunc(tripFunc TripFunc) BreakerOption
- type CircuitBreaker
- func (cb *CircuitBreaker) Counters() Counters
- func (cb *CircuitBreaker) Do(ctx context.Context, o Operation) (interface{}, error)
- func (cb *CircuitBreaker) Done(ctx context.Context, err error) error
- func (cb *CircuitBreaker) Fail()
- func (cb *CircuitBreaker) FailWithContext(ctx context.Context)
- func (cb *CircuitBreaker) Ready() bool
- func (cb *CircuitBreaker) Reset()
- func (cb *CircuitBreaker) SetState(st State)
- func (cb *CircuitBreaker) State() State
- func (cb *CircuitBreaker) Success()
- type Counters
- type IgnorableError
- type Operation
- type State
- type StateChangeHook
- type SuccessMarkableError
- type TripFunc
Examples ¶
Constants ¶
const ( DefaultInterval = 1 * time.Second DefaultHalfOpenMaxSuccesses = 4 )
Default setting parameters.
Variables ¶
var ( // ErrOpen is an error to signify that the CB is open and executing // operations are not allowed. ErrOpen = errors.New("circuit breaker open") // DefaultTripFunc is used when Options.ShouldTrip is nil. DefaultTripFunc = NewTripFuncThreshold(10) )
Functions ¶
func DefaultOpenBackOff ¶
DefaultOpenBackOff returns defaultly used BackOff.
func MarkAsSuccess ¶
MarkAsSuccess wraps the given err in a *SuccessMarkableError.
Types ¶
type BreakerOption ¶
type BreakerOption interface {
// contains filtered or unexported methods
}
BreakerOption interface for applying configuration in the constructor
func WithCounterResetInterval ¶
func WithCounterResetInterval(interval time.Duration) BreakerOption
WithCounterResetInterval Set the interval of the circuit breaker, which is the cyclic time period to reset the internal counters
func WithFailOnContextCancel ¶
func WithFailOnContextCancel(failOnContextCancel bool) BreakerOption
WithFailOnContextCancel Set if the context should fail on cancel
func WithFailOnContextDeadline ¶
func WithFailOnContextDeadline(failOnContextDeadline bool) BreakerOption
WithFailOnContextDeadline Set if the context should fail on deadline
func WithHalfOpenMaxSuccesses ¶
func WithHalfOpenMaxSuccesses(maxSuccesses int64) BreakerOption
WithHalfOpenMaxSuccesses Set the number of half open successes
func WithOnStateChangeHookFn ¶
func WithOnStateChangeHookFn(hookFn StateChangeHook) BreakerOption
WithOnStateChangeHookFn set a hook function that trigger if the condition of the StateChangeHook is true
func WithOpenTimeout ¶
func WithOpenTimeout(timeout time.Duration) BreakerOption
WithOpenTimeout Set the timeout of the circuit breaker
func WithOpenTimeoutBackOff ¶
func WithOpenTimeoutBackOff(backoff backoff.BackOff) BreakerOption
WithOpenTimeoutBackOff Set the time backoff
func WithTripFunc ¶
func WithTripFunc(tripFunc TripFunc) BreakerOption
WithTripFunc Set the function for counter
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker provides circuit breaker pattern.
Example ¶
cb := circuitbreaker.New(nil) ctx := context.Background() data, err := cb.Do(context.Background(), func() (interface{}, error) { user, err := fetchUserInfo(ctx, "太郎") if err != nil && err.Error() == "UserNoFound" { // If you received a application level error, wrap it with Ignore to // avoid false-positive circuit open. return nil, circuitbreaker.Ignore(err) } return user, err }) if err != nil { log.Fatalf("failed to fetch user:%s\n", err.Error()) } log.Printf("fetched user:%+v\n", data.(*user))
Output:
func New ¶
func New(opts ...BreakerOption) *CircuitBreaker
New returns a new CircuitBreaker The constructor will be instanced using the functional options pattern. When creating a new circuit breaker we should pass or left it blank if we want to use its default options. An example of the constructor would be like this:
cb := circuitbreaker.New(
circuitbreaker.WithClock(clock.New()), circuitbreaker.WithFailOnContextCancel(true), circuitbreaker.WithFailOnContextDeadline(true), circuitbreaker.WithHalfOpenMaxSuccesses(10), circuitbreaker.WithOpenTimeoutBackOff(backoff.NewExponentialBackOff()), circuitbreaker.WithOpenTimeout(10*time.Second), circuitbreaker.WithCounterResetInterval(10*time.Second), // we also have NewTripFuncThreshold and NewTripFuncConsecutiveFailures circuitbreaker.WithTripFunc(circuitbreaker.NewTripFuncFailureRate(10, 0.4)), circuitbreaker.WithOnStateChangeHookFn(func(from, to circuitbreaker.State) { log.Printf("state changed from %s to %s\n", from, to) }),
)
The default options are described in the defaultOptions function
func (*CircuitBreaker) Counters ¶
func (cb *CircuitBreaker) Counters() Counters
Counters returns internal counters. If current status is not StateClosed, returns zero value.
func (*CircuitBreaker) Do ¶
func (cb *CircuitBreaker) Do(ctx context.Context, o Operation) (interface{}, error)
Do executes the Operation o and returns the return values if cb.Ready() is true. If not ready, cb doesn't execute f and returns ErrOpen.
If o returns a nil-error, cb counts the execution of Operation as a success. Otherwise, cb count it as a failure.
If o returns a *IgnorableError, Do() ignores the result of operation and returns the wrapped error.
If o returns a *SuccessMarkableError, Do() count it as a success and returns the wrapped error.
If given Options' FailOnContextCancel is false (default), cb.Do doesn't mark the Operation's error as a failure if ctx.Err() returns context.Canceled.
If given Options' FailOnContextDeadline is false (default), cb.Do doesn't mark the Operation's error as a failure if ctx.Err() returns context.DeadlineExceeded.
func (*CircuitBreaker) Done ¶
func (cb *CircuitBreaker) Done(ctx context.Context, err error) error
Done is a helper function to finish the protected operation. If err is nil, Done calls Success and returns nil. If err is a SuccessMarkableError or IgnorableError, Done returns wrapped error. Otherwise, Done calls FailWithContext internally.
func (*CircuitBreaker) Fail ¶
func (cb *CircuitBreaker) Fail()
Fail signals that an execution of operation has been failed to cb.
func (*CircuitBreaker) FailWithContext ¶
func (cb *CircuitBreaker) FailWithContext(ctx context.Context)
FailWithContext calls Fail internally. But if FailOnContextCancel is false and ctx is done with context.Canceled error, no Fail() called. Similarly, if FailOnContextDeadline is false and ctx is done with context.DeadlineExceeded error, no Fail() called.
func (*CircuitBreaker) Ready ¶
func (cb *CircuitBreaker) Ready() bool
Ready reports if cb is ready to execute an operation. Ready does not give any change to cb.
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets cb's state with StateClosed.
func (*CircuitBreaker) SetState ¶
func (cb *CircuitBreaker) SetState(st State)
SetState set state of cb to st.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State reports the curent State of cb.
func (*CircuitBreaker) Success ¶
func (cb *CircuitBreaker) Success()
Success signals that an execution of operation has been completed successfully to cb.
type Counters ¶
type Counters struct { Successes int64 Failures int64 ConsecutiveSuccesses int64 ConsecutiveFailures int64 }
Counters holds internal counter(s) of CircuitBreaker.
type IgnorableError ¶
type IgnorableError struct {
// contains filtered or unexported fields
}
IgnorableError signals that the operation should not be marked as a failure.
func (*IgnorableError) Error ¶
func (e *IgnorableError) Error() string
type StateChangeHook ¶
type StateChangeHook func(oldState, newState State)
StateChangeHook is a function which will be invoked when the state is changed.
type SuccessMarkableError ¶
type SuccessMarkableError struct {
// contains filtered or unexported fields
}
SuccessMarkableError signals that the operation should be mark as success.
func (*SuccessMarkableError) Error ¶
func (e *SuccessMarkableError) Error() string
func (*SuccessMarkableError) Unwrap ¶
func (e *SuccessMarkableError) Unwrap() error
Unwrap unwraps e.
type TripFunc ¶
TripFunc is a function to determine if CircuitBreaker should open (trip) or not. TripFunc is called when cb.Fail was called and the state was StateClosed. If TripFunc returns true, the cb's state goes to StateOpen.
func NewTripFuncConsecutiveFailures ¶
NewTripFuncConsecutiveFailures provides a TripFunc that returns true if the consecutive failures is larger than or equals to threshold.
func NewTripFuncFailureRate ¶
NewTripFuncFailureRate provides a TripFunc that returns true if the failure rate is higher or equals to rate. If the samples are fewer than min, always returns false.
func NewTripFuncThreshold ¶
NewTripFuncThreshold provides a TripFunc. It returns true if the Failures counter is larger than or equals to threshold.