mingalevme/retryable-psr-http-client
Composer 安装命令:
composer require mingalevme/retryable-psr-http-client
包简介
The package provides a familiar PSR HTTP Client interface with automatic retries and exponential backoff (and others strategies). It is a thin wrapper over the standard PSR HTTP Client and exposes same public API. This makes the decorator very easy to drop into existing programs.
关键字:
README 文档
README
Simple Retryable Psr Http Client Decorator with Retry-After-header support* and 100% code coverage.
NOTE: Retry-After-header handling is disabled by default because relying on untrusted headers it risky and dangerous, turn it on only if you clearly understand the consequences.
Composer
composer require mingalevme/retryable-psr-http-client
Example 1 (Simple drop-in replacement)
- Max 3 attempts.
- Triggers on 5xx/429 response status codes and/or
Psr\Http\Client\ClientExceptionInterface. - Exponential backoff: 2 power of attempt number (1, 2, 4, 8, ...).
use Mingalevme\RetryablePsrHttpClient\RetryablePsrHttpClient; use Psr\Http\Client\ClientInterface; $someDiContainer->decorate(ClientInterface::class, function (ClientInterface $client): RetryablePsrHttpClient { return new RetryablePsrHttpClient($client); });
Example 2 (Extended Usage)
- Max 5 attempts.
- Respect Retry-After-header.
- Liner backoff with 1s (initial value) + 2s (slope).
- Triggers on 4xx, 5xx and
Psr\Http\Client\ClientExceptionInterface. - Log on any error (unacceptable response or
Psr\Http\Client\ClientExceptionInterface-exception).
<?php use Mingalevme\RetryablePsrHttpClient\BackoffCalc\LinearBackoffCalc; use Mingalevme\RetryablePsrHttpClient\Config; use Mingalevme\RetryablePsrHttpClient\NullEventListener; use Mingalevme\RetryablePsrHttpClient\ResponseAnalyzer\ResponseAnalyzerInterface; use Mingalevme\RetryablePsrHttpClient\RetryablePsrHttpClient; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; final class MyAppHttpClientErrLogEventListener extends AbstractEventListener { public function __construct( private LoggerInterface $logger, ) { } public function onError( int $attemptNumber, RequestInterface $request, ClientExceptionInterface|ResponseInterface $error, ): void { $this->logger->error("Error while sending request {$request->getUri()}, attempt #$attemptNumber"); } } final class MyAppHttpResponseAnalyzer implements ResponseAnalyzerInterface { public function isAcceptable(ResponseInterface $response): bool { return $response->getStatusCode() >= 400; } } $someDiContainer->decorate( ClientInterface::class, function ( ClientInterface $client, MyAppHttpClientErrLogEventListener $listener, MyAppHttpResponseAnalyzer $responseAnalyzer, ): RetryablePsrHttpClient { $config = Config::new() ->setRetryCount(5) ->setRespectRetryAfterHeader(true) ->setBackoffCalc(new LinearBackoffCalc(2, 1)) ->setResponseAnalyzer($responseAnalyzer) ->addEventListener($listener); return new RetryablePsrHttpClient($client, $config); }, );
mingalevme/retryable-psr-http-client 适用场景与选型建议
mingalevme/retryable-psr-http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 279 次下载、GitHub Stars 达 6, 最近一次更新时间为 2022 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「psr」 「http-client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mingalevme/retryable-psr-http-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mingalevme/retryable-psr-http-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mingalevme/retryable-psr-http-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple PSR-16 cache implementations for WordPress transient the OOP way
Mock PSR-18 HTTP client
Common interface for HTTP clients
An easy to use wrapper for the curl http library in PHP
Error handler based on Booboo with HTML and JSON support
Simple builder for the sending request based on the component symfony/http-client
统计信息
- 总下载量: 279
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-07