Excepting traffic control, circuit breaking used for unstable resource in calling link is also one of the important measures to ensure high availability. B/C the complexity of the call link in a distributed system, if a service is unstable in the calling link, the callers who depend on that service may themselves be blocked by slow calls, eventually causing the entire link to crash.
Sentinel circuit breaker will cut the calls to resource if some downstream resource occurs unstable state. the fail-fast could avoid the cascading error. If the resource was degraded, calls to the resource are automatically circuited during the next degradation time window.
Below is the state machine of circuit breaker:
the error count/error ratio/ slow request ratio exceeds the threshold
+-----------------------------------------------------------------------+
| |
| v
+----------------+ +----------------+ Probe +----------------+
| | Probe succeed | |<----------------| |
| | Recover to Closed | | | |
| Closed |<------------------| HalfOpen | | Open |
| | | | Probe failed | |
| | | +---------------->| |
+----------------+ +----------------+ +----------------+
Sentinel Go support three circuit strategy:
Note: A circuit breaker error is only for business exceptions, and the Sentinel limiter reversion itself of the exception (BlockError) is not valid. For custom burial points, you need to record business exceptions through api.traceError(err) in order to count the percentage or number of exceptions.
The demo refers to https://github.com/alibaba/sentinel-golang/blob/master/example/circuitbreaker/circuit_breaker_example.go