# Circuit Breaker ## Installation ``` composer require hyperf/circuit-breaker ``` ## Why you need a Circuit Breaker ? In distributed systems, it is often the case that the entire system is unavailable due to the unavailability of a basic service. This phenomenon is called the service avalanche effect. In response to service avalanches, a common practice is to downgrade services. The [hyperf/circuit-breaker](https://github.com/hyperf-cloud/circuit-breaker) component is designed to solve this problem. ## Usage ## Why you need a Circuit Breaker ? In distributed systems, it is often the case that the entire system is unavailable due to the unavailability of a basic service. This phenomenon is called the service avalanche effect. In response to service avalanches, a common practice is to downgrade services. The [hyperf/circuit-breaker](https://github.com/hyperf-cloud/circuit-breaker) component is designed to solve this problem. ## Using Circuit Breaker The usage of the Circuit Breaker is very simple, just add the `Hyperf\CircuitBreaker\Annotation\CircuitBreaker` annotation, you can circuit break according to the specified strategy. For example, we need to query the user list in another service. The user list needs to be associated with a lots of tables. The query efficiency is low, but when the concurrent amount is normal, the corresponding speed is still reasonable. Once the amount of concurrency increases, it will slow down the response and cause the other service to slow down. At this time, we only need to configure the circuit break timeout period `timeout` to be 0.05 seconds, the failure count `failCounter` to be blown after more than 1 time, and the corresponding `fallback` is the `searchFallback` method of the `App\UserService` class. In this way, when the response times out and triggers the circuit break, it will not request the peer's service any more. Instead, it will directly downgrade the service from the current application, that is, return according to the method specified by `fallback`. ```php client->users($offset, $limit); } public function searchFallback($offset, $limit) { return []; } } ``` The default circuit break policy is `Timeout Policy`. If you want to implement the circuit break policy yourself, you only need to implement `Handler` inherited by `Hyperf\CircuitBreaker\Handler\AbstractHandler`. ```php process(); if (is_break()) { throw new TimeoutException('timeout, use ' . $use . 's', $result); } return $result; } } ```