compwright / should-retry
PSR interop HTTP retry middleware helper
v1.0.0
2025-03-14 16:47 UTC
Requires
- php: ^8.1 || ^8.2 || ^8.3 || ^8.4
- psr/http-client: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.72
- guzzlehttp/guzzle: ^7.9
- guzzlehttp/psr7: ^2.7
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5 || 11.5 || ^12.0
This package is auto-updated.
Last update: 2025-03-14 16:49:56 UTC
README
Installation
$ composer require compwright/should-retry
Usage with Guzzle HTTP library
The popular Guzzle HTTP library includes a retry middleware:
use Compwright\ShouldRetry\ShouldRetry; use Compwright\ShouldRetry\RetryAfter; $handler = GuzzleHttp\HandlerStack::create(); $handler->push( GuzzleHttp\Middleware::retry( new ShouldRetry(), new RetryAfter() ), 'retry' ); $client = new GuzzleHttp\Client([ 'handler' => $handler, ]);
Configuration
Both ShouldRetry and RetryAfter are configurable via setters:
$shouldRetry = (new ShouldRetry()) ->setMaxRetries(5) ->setRetryOnStatusCodes(429) ->setLogger($logger); // attach PSR-3 debug logger $retryAfter = (new RetryAfter()) ->setRetryAfterHeader('x-rate-limit-reset') ->setFallbackStrategy($fallback);
Default configuration:
- Retry up to 3 times
- Retry on 429, 500, 502, 503, 504 error codes
- When retrying, wait
Retry-After
seconds - If header is missing, use exponential backoff
License
MIT License