ejsmont-artur/php-circuit-breaker
Composer 安装命令:
composer require ejsmont-artur/php-circuit-breaker
包简介
PHP Circuit Breaker component
README 文档
README
A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).
It is a library providing extremely easy to use circuit breaker component. It does not require external dependencies and it has default storage implementations for APC and Memcached but can be extended multiple ways.
Frameworks support
This library does not require any particular PHP framework, all you need is PHP 5.3 or higher.
Symfony 2
If you are using Symfony 2 framework you should use php-circuit-breaker-bundle. It is a bundle i have created to wrap up php-circuit-breaker and integrate it with Symfony 2 components and Dependency Injection.
Other Frameworks
If you are using other frameworks and you would like to use php-circuit-breaker please let me know and we can try to build up an open source integration just like the one for Symfony 2.
Motivation & Benefits
- Allow application to detect failures and adapt its behaviour without human intervention.
- Increase robustness of services by addinf fail-safe functionality into modules.
Installation
You can download sources and use them with your autoloader or you can use composer in which case all you nees is a require like this:
"require": {
"ejsmont-artur/php-circuit-breaker": "*"
},
After that you should update composer dependencies and you are good to go.
Use Case - Non-Critical Feature
- Your application has an Non-Critical Feature like: user tracking, stats, recommendations etc
- The optional feature uses remote service that causes outages of your application.
- You want to keep applicaton and core processes available when "Non-Critical Feature" fails.
Code of your application could look something like:
$factory = new Ejsmont\CircuitBreaker\Factory();
$circuitBreaker = $factory->getSingleApcInstance(30, 300);
$userProfile = null;
if( $circuitBreaker->isAvailable("UserProfileService") ){
try{
$userProfile = $userProfileService->loadProfileOrWhatever();
$circuitBreaker->reportSuccess("UserProfileService");
}catch( UserProfileServiceConnectionException $e ){
// network failed - report it as failure
$circuitBreaker->reportFailure("UserProfileService");
}catch( Exception $e ){
// something went wrong but it is not service's fault, dont report as failure
}
}
if( $userProfile === null ){
// for example, show 'System maintenance, you cant login now.' message
// but still let people buy as logged out customers.
}
Use Case - Payment Gateway
- Web application depends on third party service (for example a payment gateway).
- Web application needs to keep track when 3rd party service is unavailable.
- Application can not become slow/unavailable, it has to tell user that features are limited or just hide them.
- Application uses circuit breaker before checkout page rendering and if particular payment gateway is unavailable payment option is hidden from the user.
As you can see that is a very powerful concept of selectively disabling feautres at runtime but still allowing the core business processes to be uninterrupted.
Backend talking to the payment service could look like this:
$factory = new Ejsmont\CircuitBreaker\Factory();
$circuitBreaker = $factory->getSingleApcInstance(30, 300);
try{
// try to process the payment
// then tell circuit breaker that it went well
$circuitBreaker->reportSuccess("PaymentOptionOne");
}catch( SomePaymentConnectionException $e ){
// If you get network error report it as failure
$circuitBreaker->reportFailure("PaymentOptionOne");
}catch( Exception $e ){
// in case of your own error handle it however it makes sense but
// dont tell circuit breaker it was 3rd party service failure
}
Since you are recording failed and successful operations you can now use them in the front end as well to hide payment options that are failing.
Frontend rendering the available payment options could look like this:
$factory = new Ejsmont\CircuitBreaker\Factory();
$circuitBreaker = $factory->getSingleApcInstance(30, 300);
if ($circuitBreaker->isAvailable("PaymentOptionOne")) {
// display the option
}
Features
- Track multiple services through a single Circuit Breaker instance.
- Pluggable backend adapters, provided APC and Memcached by default.
- Customisable service thresholds. You can define how many failures are necessary for service to be considered down.
- Customisable retry timeout. You do not want to disable the service forever. After provided timeout circuit breaker will allow a single process to attempt
Performance Impact
Overhead of the Circuit Breaker is negligible.
APC implementation takes roughly 0.0002s to perform isAvailable() and then reportSuccess() or reportFailure().
Memcache adapter is in range of 0.0005s when talking to the local memcached process.
The only potential performance impact is network connection time. If you chose to use remote memcached server or implement your own custom StorageAdapter.
Running tests
- Tests are run via PHPUnit It is assumed to be installed via PEAR.
- Tests can be ran using phpunit alone or via ant build targets.
- The "ci" target generate code coverage repor, "phpunit" target does not.
You can run all tests by any of the following:
ant
ant phpunit
ant ci
You can run selected test case by running:
cd tests
phpunit Unit/Ejsmont/CircuitBreaker/Storage/Adapter/DummyAdapterTest.php
Details
Before documentation gets updated you can read more on the concept of circuit breaker and its implementation on my blog http://artur.ejsmont.org/blog/circuit-breaker
Some implementation details has changed but the core logic is still the same.
Author
- Artur Esjmont (https://github.com/ejsmont-artur) via http://artur.ejsmont.org
ejsmont-artur/php-circuit-breaker 适用场景与选型建议
ejsmont-artur/php-circuit-breaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1M 次下载、GitHub Stars 达 168, 最近一次更新时间为 2013 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「error handling」 「circuit breaker」 「graceful」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ejsmont-artur/php-circuit-breaker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ejsmont-artur/php-circuit-breaker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ejsmont-artur/php-circuit-breaker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A circuit breaker pattern implementation for the Laravel framework
Circuit Breaker pattern implementation in PHP
Provides form handler for Symfony form types
PHP Error Handler module that captures and displays all throwable errors in a given format, making debugging easier and more efficient
Circuit Breaker pattern implementation in Laravel 5.
The Laravel implementation for The Circuit breaker pattern
统计信息
- 总下载量: 1M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 169
- 点击次数: 21
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-02-23