Skip to main content

Circuit Breaker

Circuit breaker middleware for providing client-side breaker functionality, with sre breaker algorithm implemented by default。



breaker depends on container/group to implement the use of mutually independent breaker for different Operation. use WithGroup to configure a costom group to replace the default breaker algorithm:

// WithGroup with circuit breaker group.
// NOTE: implements generics circuitbreaker.CircuitBreaker
func WithGroup(g *group.Group) Option {
return func(o *options) { = g

The default configuration generates separate breakers for different Operation(s).

opt := &options{
group: group.NewGroup(func() interface{} {
return sre.NewBreaker()


Use circuit breaker in client

conn, err := http.NewClient(

Trigger circuit breaker

When the breaker is triggered, the client call for this Operation fails quickly for a period of time and returns the error ErrNotAllowed immediately,which defined as follows:

// ErrNotAllowed is request failed due to circuit breaker triggered.
var ErrNotAllowed = errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered")