flavioheleno / interrupt
符合PSR-18规范的断路器包装器
Requires
- php: ^8.2
- flavioheleno/scale: ^0.2.2
- psr/cache: ^3.0
- psr/clock: ^1.0
- psr/http-client: ^1.0
- psr/http-message: ^1.0 | ^2.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.8
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.0
- psy/psysh: ^0.12
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-11 18:04:08 UTC
README
该库实现了一个符合PSR-18规范的断路器包装器,可用于确保服务稳定性,防止远程服务调用超载或退化。
致谢
本库深受以下库的启发:
安装
要使用Interim,请使用composer进行安装
composer require flavioheleno/interrupt
用法
作为PSR-18 HTTP客户端包装器
// any PSR-18 compliant HTTP Client implementation // $httpClient = new ... // resolves the service name to its scheme + host + port // eg. https://api.example.org/v1 -> https://api.example.org // eg. https://api.example.org:5000/v1 -> https://api.example.org:5000 // can be replaced by StaticNameResolver $serviceNameResolver = Interrupt\ServiceNameResolvers\UriBasedResolver(); // any PSR-6 compliant Cache Item Pool implementation // $cacheItemPool = new ... // any PSR-20 compliant Clock implementation // $clock = new ... // can be replaced by FixedTimeWindowBasedRecordStrategy $recordStrategy = new Interrupt\RecordStrategies\SlidingTimeWindowBasedRecordStrategy( $clock ); // can be replaced by CountBasedCircuitBreaker $circuitBreaker = new Interrupt\CircuitBreakers\RateBasedCircuitBreaker( $clock, $cacheItemPool, $recordStrategy ); $failureDetector = new Interrupt\FailureDetectors\HttpStatusBasedFailureDetector(); $client = new Psr18Wrapper( $httpClient, $serviceNameResolver, $circuitBreaker, $failureDetector ); // when the called service is unavailable, $client will throw an // Interrupt\Exceptions\ServiceUnavailableException exception.
作为Guzzle中间件
$middleware = new GuzzleMiddleware( $serviceNameResolver, $circuitBreaker, $failureDetector ); $handlerStack = \GuzzleHttp\HandlerStack::create(); $handlerStack->push($middleware); $client = new GuzzleHttp\Client(['handler' => $handlerStack]); // when the called service is unavailable, $client will throw an // Interrupt\Exceptions\ServiceUnavailableException exception.
组件
Interrupt围绕组件的概念构建,以允许轻松集成不同的环境或框架,使其成为一个灵活且可定制的库。
服务名称解析器
为了跟踪包装客户端访问的服务,Interrupt使用服务名称解析器的概念,基于请求属性生成一致的服务名称。
Interrupt附带以下解析器:
- UriBasedResolver,从请求URI组件生成服务名称的解析器实现
- StaticNameResolver,始终返回一个固定服务名称的解析器实现
记录策略
记录策略决定了Interrupt在一段时间内如何跟踪失败事件。
FixedTimeWindowBasedRecordStrategy使用预定义的时间间隔在其持续时间内在其内部注册失败记录。一旦间隔结束,记录的失败就会被清理,并开始一个新的固定间隔。
SlidingTimeWindowBasedRecordStrategy使用移动或移动的时间间隔来注册失败记录。与固定间隔不同,时间窗口随着失败流滑动(或移动)。
滑动时间窗口方法允许对最近的数据进行连续分析,同时仍考虑特定的时间范围。
注意
这两种策略都需要一个兼容PSR-20的时钟实现。
故障检测器
故障可能取决于上下文,因此Interrupt依赖于故障检测器来检测上下文相关的故障。
在Psr18Wrapper中嵌入了对网络问题、超时以及任何扩展Psr\Http\Client\ClientExceptionInterface
的抛出异常的故障检测。
此外,HttpStatusBasedFailureDetector将HTTP状态码解释为失败的信号。
断路器
Interrupt附带两个断路器实现
一个监控记录失败次数的CountBasedCircuitBreaker,如果超过阈值则会自动中断请求流。
一个监控记录失败率或频率的RateBasedCircuitBreaker,如果错误率超过预定义的阈值则会自动中断请求流。
注意
这两种熔断器都需要一个与PSR-6兼容的缓存实现。
许可证
此库根据MIT许可证授权。