Circuit Breaker
About 1 mincomponentcircuit breakerservice degradation
Overview
Fuse mechanism is a fault-tolerant mechanism that automatically cuts off traffic when service fails or response time is too long to protect system availability and stability. Support degradation treatment after fuse, reduce system loss.
Enable Circuit Breaker
Configuration
In Web or gRPC services created by Sponge, the adaptive circuit breaker function is disabled by default. To enable it, please set the following in the YAML configuration file under the configs
directory:
app:
enableCircuitBreaker: true # Whether to enable adaptive circuit breaker, true: enable, false: disable
In Web Services Settings
Default Circuit Breaking Conditions:
- Triggers circuit breaking only for HTTP 500 and 503 error codes
Custom Configuration:
- Extensible range of error codes that trigger circuit breaking
- Supports custom fallback logic
- Configuration location:
internal/routers/routers.go
// circuit breaker middleware if config.Get().App.EnableCircuitBreaker { r.Use(middleware.CircuitBreaker( //middleware.WithBreakerOption( //circuitbreaker.WithSuccess(75), // default 60 //circuitbreaker.WithRequest(100), // default 100 //circuitbreaker.WithBucket(20), // default 10 //circuitbreaker.WithWindow(time.Second*3), // default 3s //), //middleware.WithDegradeHandler(handler), // Add degradation processing //middleware.WithValidCode(http.StatusRequestTimeout), // Add timeout error codes can also trigger circuit breaking )) }
In gRPC Services Settings
Default Circuit Breaking Conditions:
- Triggers circuit breaking only for gRPC status codes Internal (13) and Unavailable (14)
Custom Configuration:
- Extensible range of error codes that trigger circuit breaking
- Supports custom fallback logic
- Configuration location:
internal/server/grpc.go
// circuit breaker interceptor if config.Get().App.EnableCircuitBreaker { unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerCircuitBreaker( //interceptor.WithBreakerOption( //circuitbreaker.WithSuccess(75), // default 60 //circuitbreaker.WithRequest(100), // default 100 //circuitbreaker.WithBucket(10), // default 10 //circuitbreaker.WithWindow(time.Second*3), // default 3s //), //interceptor.WithUnaryServerDegradeHandler(handler), // Add degradation processing //interceptor.WithValidCode(codes.DeadlineExceeded), // Add timeout error codes can also trigger circuit breaking )) }