Skip to content

Circuit Breaker

go-zero integrates a Google SRE-style circuit breaker into every RPC client and HTTP service automatically — no manual wiring required.

The breaker tracks the ratio of errors over a sliding window. When the error rate exceeds a threshold, the circuit opens and requests fail immediately (fast-fail) instead of waiting for a slow upstream.

StateBehaviour
ClosedNormal operation, errors tracked
OpenRequests rejected immediately
Half-OpenProbe requests allowed; recovers or re-opens

The circuit breaker activates automatically for all zrpc calls — nothing to configure.

// This call is automatically protected
resp, err := l.svcCtx.OrderRpc.CreateOrder(l.ctx, req)
if err != nil {
// err may be breaker.ErrServiceUnavailable during open state
return nil, err
}
import "github.com/zeromicro/go-zero/core/breaker"
b := breaker.NewBreaker()
err := b.Do(func() error {
return callExternalAPI()
})
// or with custom accept/reject functions:
err = b.DoWithAcceptable(func() error {
return callExternalAPI()
}, func(err error) bool {
// return true to not count this error against the breaker
return errors.Is(err, ErrNotFound)
})

The built-in breaker uses sensible defaults. To tune:

b := breaker.NewBreaker(breaker.WithName("payment-service"))

Metrics are exported to Prometheus automatically when Prometheus is enabled in config.