postcon/resilience
Composer 安装命令:
composer require postcon/resilience
包简介
Library of resilience helpers
README 文档
README
A collection of reusable resilience pattern implementations. Currently implemented:
Installation
Using composer:
composer require postcon/resilience
Simple usage
$redis = new \Redis(); $circuitBreaker = new \Postcon\Resilience\RedisCircuitBreaker($redis, 'system', 120, 3); $circuitBreaker->reportSuccess(); $circuitBreaker->isAvailable(); // should be true $circuitBreaker->reportFailure(); $circuitBreaker->isAvailable(); // ... still true $circuitBreaker->reportFailure(); $circuitBreaker->isAvailable(); // ... still true $circuitBreaker->reportFailure(); $circuitBreaker->isAvailable(); // ... now it is false $circuitBreaker->check(); // throws CircuitBreakerTripped exception, if 'system' is not available.
State transitions
The circuit breaker can be on one of three states: CLOSED (system is available), HALF OPEN (system is still available) and OPEN (system is not available).
The normal state of the circuit breaker is CLOSED; i.e. the system is working correctly. If a failure is reported, the state changes to HALF OPEN. If either a success is reported, or a defined time exceeds (lifetime), the state becomes CLOSED again. If failure is reported repeatedly (maxErrors), the state changes from HALF OPEN to OPEN (the circuit breaker is tripped).
The OPEN state changes back to CLOSED, after exceeding a defined time. Depending on the usage of this circuit breaker implementation, a reported success could change the OPEN state to be CLOSED as well.
-------------------------- ::reportSuccess() ---------------------------
| CLOSED | <-------------------------- | OPEN |
| ::isAvailable() === true | exceeding lifetime | ::isAvailable() === false |
-------------------------- ---------------------------
^ | ^
| | ::reportFailure() |
| ------------------------- |
| | |
| v |
| ::reportSuccess() -------------------------- |
-------------------- | HALF OPEN | ----------------------
exceeding lifetime | ::isAvailable() === true | rpt. ::reportFailure()
--------------------------
Examples
This circuit breaker implementation can be used to decorate e.g. guzzle http client:
use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\ServerException; use Postcon\Resilience\CircuitBreakerInterface; use Postcon\Resilience\CircuitBreakerTripped; use Psr\Http\Message\RequestInterface; class CircuitBreakerClientDecorator implements ClientInterface { /** @var ClientInterface */ private $baseClient; /** @var CircuitBreakerInterface */ private $circuitBreaker; public function __construct(ClientInterface $baseClient, CircuitBreakerInterface $circuitBreaker) { $this->baseClient = $baseClient; $this->circuitBreaker = $circuitBreaker; } /** * @inheritdoc * * @throws CircuitBreakerTripped */ public function send(RequestInterface $request, array $options = []) { return $this->check(function () use ($request, $options) { $this->baseClient->send($request, $options); }); } // ... /** * @throws GuzzleException * @throws CircuitBreakerTripped */ private function check(callable $function) { $this->circuitBreaker->check(); try { $result = $function(); $this->circuitBreaker->reportSuccess(); return $result; } catch (ConnectException $e) { $this->circuitBreaker->reportFailure(); throw $e; } catch (ServerException $e) { $this->circuitBreaker->reportFailure(); throw $e; } catch (ClientException $e) { $this->circuitBreaker->reportSuccess(); throw $e; } } }
Implementation details
Currently, there is a redis implementation of the circuit breaker pattern. An instance of a circuit breaker is persisted as the respective failure counter, where the redis key is the circuit breaker name.
To implement the lifetime feature (automatic state transition to CLOSED after some time), the redis EXPIRE command is used.
License
All contents of this package are licensed under the MIT license.
postcon/resilience 适用场景与选型建议
postcon/resilience 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 596 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 postcon/resilience 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 postcon/resilience 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 596
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-01