Circuit Breaker
Circuit Breaker
Section titled “Circuit Breaker”go-zero integrates a Google SRE-style circuit breaker into every RPC client and HTTP service automatically — no manual wiring required.
How It Works
Section titled “How It Works”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.
| State | Behaviour |
|---|---|
| Closed | Normal operation, errors tracked |
| Open | Requests rejected immediately |
| Half-Open | Probe requests allowed; recovers or re-opens |
Automatic Mode (RPC Clients)
Section titled “Automatic Mode (RPC Clients)”The circuit breaker activates automatically for all zrpc calls — nothing to configure.
// This call is automatically protectedresp, err := l.svcCtx.OrderRpc.CreateOrder(l.ctx, req)if err != nil { // err may be breaker.ErrServiceUnavailable during open state return nil, err}Manual Usage
Section titled “Manual Usage”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)})Configuration
Section titled “Configuration”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.