stfn/php-circuit-breaker
Composer 安装命令:
composer require stfn/php-circuit-breaker
包简介
This is circuit-breaker pattern implemented in PHP
README 文档
README
This package provides an implementation of the circuit breaker pattern in PHP. You can find more info about it here.
Installation
You can install the package via composer:
composer require stfn/php-circuit-breaker
Usage
Wrap your potentially error-prone function with the circuit breaker, and it will monitor and handle failures.
use Stfn\CircuitBreaker\CircuitBreaker; $result = CircuitBreaker::for('3rd-party-service')->call(function () { // Your function that could fail });
States
Circuit breaker can have 4 different states.
Closed
In the Closed state, the circuit breaker is fully operational, allowing calls to the 3rd party service. Any exceptions that occur during this state are counted.
Half Open
The Half Open state is a transitional phase where the circuit breaker allows a limited number of calls to the 3rd party service. If these calls are successful, the circuit is closed again. However, if the service continues to exhibit issues, the circuit is moved back to the Open state.
Open
The Open state indicates that the circuit breaker has detected a critical failure, and the call method will fail immediately, throwing a CircuitOpenException exception.
Force Open
Force Open is not part of the regular flow. It can be utilized when intentional suspension of calls to a service is required. In this state, a CircuitOpenException will be thrown.
To force the circuit breaker into the Force Open state, use the following:
use Stfn\CircuitBreaker\CircuitBreaker; $breaker = CircuitBreaker::for('3rd-party-service')->forceOpenCircuit();
This feature provides a manual override to stop calls to a service temporarily, offering additional control when needed.
Storage
By default, the circuit breaker uses InMemoryStorage as a storage driver, which is not suitable for most of PHP applications.
More useful would be to use RedisStorage.
use Stfn\CircuitBreaker\Storage\RedisStorage; use Stfn\CircuitBreaker\CircuitBreaker; $redis = new \Redis(); $redis->connect("127.0.0.1"); $storage = new RedisStorage($redis); $result = CircuitBreaker::for('3rd-party-service') ->storage($storage) ->call(function () { // Your function that could fail });
You could also write your implementation of storage. You should just implement CircuitBreakerStorage interface.
Configuration
Each circuit breaker has default configuration settings, but you can customize them to fit your needs:
use Stfn\CircuitBreaker\CircuitBreaker; $breaker = CircuitBreaker::for('3rd-party-service') ->withOptions([ 'failure_threshold' => 10, // Number of failures triggering the transition to the open state 'recovery_time' => 120, // Time in seconds to keep the circuit breaker open before attempting recovery 'sample_duration' => 60, // Duration in seconds within which failures are counted 'consecutive_success' => 3 // Number of consecutive successful calls required to transition from half open to closed state ]);
Excluding some failures
Change the circuit breaker behavior by configuring it to exclude certain types of failures:
use Stfn\CircuitBreaker\CircuitBreaker; $breaker = CircuitBreaker::for('3rd-party-service') ->skipFailureCount(function ($exception) { return $exception->getCode() < 500; });
Listeners
You can add listeners for circuit breaker actions by extending the CircuitBreakerListener class:
use Stfn\CircuitBreaker\CircuitBreakerListener; use Stfn\CircuitBreaker\CircuitState; class LoggerListener extends CircuitBreakerListener { public function beforeCall(CircuitBreaker $breaker, \Closure $action,...$args) : void { Log::info("before call"); } public function onSuccess(CircuitBreaker $breaker, $result): void { Log::info($result); } public function onFail(CircuitBreaker $breaker, $exception) : void { Log::info($exception); } public function onStateChange(CircuitBreaker $breaker, CircuitState $previousState, CircuitState $newState) { Log::info($previousState, $newState); } }
Attach the listener to the circuit breaker:
use Stfn\CircuitBreaker\CircuitBreaker; $breaker = CircuitBreaker::for('3rd-party-service') ->listeners([new LoggerListener()]);
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
stfn/php-circuit-breaker 适用场景与选型建议
stfn/php-circuit-breaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「stfndamjanovic」 「circuit-breaker-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stfn/php-circuit-breaker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stfn/php-circuit-breaker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stfn/php-circuit-breaker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-25