poor-plebs / guzzle-connect-retry-decider
一个guzzle重试中间件决策器,在无法建立连接时重新尝试请求。对于GET请求,总是重试x次,在特定条件下也适用于其他HTTP方法。
1.1.1
2022-12-21 20:54 UTC
Requires
- php: ^8.1.0
- guzzlehttp/guzzle: ^6.5.8 || ^7.4.5
- psr/http-message: ^1.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13.0
- phpstan/phpstan: ^1.9.0
- phpunit/phpunit: ^9.5.26
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-04 20:37:19 UTC
README
它是用来做什么的? | 有什么要求? | 如何安装它? | 如何使用它? | 如何贡献?
一个guzzle重试中间件决策器,在无法建立连接时重新尝试请求。对于GET请求,总是重试x次,在特定条件下也适用于其他HTTP方法。
它是用来做什么的?
为了更好地抵抗各种连接问题,简单地重试请求是一种好的做法。Guzzle http包已经自带了一个接受决策调用器的通用重试中间件。
此包提供了一个决策器,当抛出Guzzle连接异常时,将重试请求最多x次。对于GET请求,决策器将始终重试。对于其他HTTP方法,决策器只有在尚未建立连接(未发送数据,对于HTTPS未完成握手)的情况下才会重试,以防止潜在的重复发送事件。
有什么要求?
- PHP 8.1或更高版本
如何安装它?
composer require poor-plebs/guzzle-connect-retry-decider
如何使用它?
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; $handlerStack = HandlerStack::create(); // Where to put this middleware in the middleware stack depends on your usecase. // Usually just before the handler on top or before a log middleware. $handlerStack->push( Middleware::retry(new ConnectRetryDecider( maxRetries: 3, onBeforeRetry: function ( int $retries, RequestInterface $request, Throwable $exception ): void { /* Optional closure that is executed just before the retry is done. * At this point it is already decided that we will retry. * * Can be used to log the following retry or do some other action. */ } )), 'connect_retry', ); $client = new Client([ 'base_uri' => 'https://sometest.com/', 'handler' => $handlerStack, ]); $client->getAsync('information')->wait();
`maxRetries` 和 `onBeforeRetry` 都是可选的。最大重试次数默认为3次。如果提供了,则 `onBeforeRetry` 将在重试之前执行。回调接收已执行的尝试次数、请求实例和导致前一次尝试失败的错误。
如何贡献?
poor-plebs/guzzle-connect-retry-decider
遵循语义版本控制。更多关于 semver.org。
创建问题报告问题或请求。Fork并创建拉取请求以提出解决方案和想法。始终在在未发布部分添加CHANGELOG.md条目。