PHPackages                             caseyamcl/guzzle\_retry\_middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. caseyamcl/guzzle\_retry\_middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

caseyamcl/guzzle\_retry\_middleware
===================================

Guzzle v6+ retry middleware that handles 429/503 status codes and connection timeouts

v2.13.0(10mo ago)21810.7M—7.7%32[6 issues](https://github.com/caseyamcl/guzzle_retry_middleware/issues)20MITPHPPHP ^7.1|^8.0CI passing

Since Jul 30Pushed 10mo ago4 watchersCompare

[ Source](https://github.com/caseyamcl/guzzle_retry_middleware)[ Packagist](https://packagist.org/packages/caseyamcl/guzzle_retry_middleware)[ Docs](https://github.com/caseyamcl/guzzle_retry_middleware)[ GitHub Sponsors](https://github.com/caseyamcl)[ RSS](/packages/caseyamcl-guzzle-retry-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (22)Used By (20)

Guzzle Retry Middleware
=======================

[](#guzzle-retry-middleware)

[![Latest Version on Packagist](https://camo.githubusercontent.com/137d68fead90bc12a274ceb9cfbc429a581368b8cbf4efd48d74a681378d3c13/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6361736579616d636c2f67757a7a6c655f72657472795f6d6964646c65776172652e7376673f7374796c653d666c6174)](https://packagist.org/packages/caseyamcl/guzzle_retry_middleware)[![Software License](https://camo.githubusercontent.com/f251623e510f5909f16ae3f4e6e548dac11340b9fde1a99be26b015b39272c00/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c6174)](LICENSE.md)[![Github Build](https://github.com/caseyamcl/guzzle_retry_middleware/workflows/Github%20Build/badge.svg)](https://github.com/caseyamcl/guzzle_retry_middleware/actions?query=workflow%3A%22Github+Build%22)[![Code coverage](https://github.com/caseyamcl/guzzle_retry_middleware/raw/master/coverage.svg)](coverage.svg)[![PHPStan Level 8](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)](https://phpstan.org/)[![Total Downloads](https://camo.githubusercontent.com/99b4e3d6ef5f482ad40a575f13ad3cc7c3ec9c97f268be168d02d055920301ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6361736579616d636c2f67757a7a6c655f72657472795f6d6964646c65776172652e7376673f7374796c653d666c6174)](https://packagist.org/packages/caseyamcl/guzzle_retry_middleware)

This is a [Guzzle v6/7+](https://docs.guzzlephp.org) middleware library that implements automatic retry of requests when HTTP servers respond with `503` or `429` status codes. It can also be configured to retry requests that timeout.

If a server supplies a [Retry-After header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After), this middleware will delay subsequent requests per the server's instructed wait period.

Unlike the built-in `RetryAfter` middleware, this middleware provides some default behavior for negotiating retries based on rules in the HTTP Spec. You can drop it right into your request stack without any additional configuration.

Features, at-a-glance:

- Automatically retries HTTP requests when a server responds with a 429 or 503 status (or any HTTP status code; this is configurable)
- Sets a retry delay based on the `Retry-After` HTTP header, if it is sent, or automatically backs off exponentially if no `Retry-After` header is sent (also configurable)
- Optionally retries requests that time out (via the `connect_timeout` or `timeout` options)
- Set an optional callback when a retry occurs (useful for logging/reporting)
- Specify a maximum number of retry attempts before giving up (default: 10)
- Near-100% test coverage, good inline documentation, and PSR-12 compliant

Install
-------

[](#install)

Via Composer

```
$ composer require caseyamcl/guzzle_retry_middleware
```

Usage
-----

[](#usage)

Basically:

```
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleRetry\GuzzleRetryMiddleware;

$stack = HandlerStack::create();
$stack->push(GuzzleRetryMiddleware::factory());

$client = new Client(['handler' => $stack]);

// Make requests galore...
```

This is the default configuration. If a HTTP server responds with a `429` or `503` status, this middleware with intercept the response and retry it up to 10 times before giving up and doing whatever behavior Guzzle would do by default (by default, throwing a `BadResponseException`).

If the server provides a `RetryAfter` header, this middleware will wait the specified time before attempting a request again. If not, then it will back off, waiting longer each time between requests until giving up after 10 attempts.

### Options

[](#options)

The following options are available:

OptionTypeDefaultSummary`retry_enabled`booleantrueIs retry enabled (useful for disabling per individual request)`max_retry_attempts`integer10Maximum number of retries per request`max_allowable_timeout_secs`integernullIf set, specifies a hard ceiling in seconds that the client can wait between requests`give_up_after_secs`integernullIf set, specifies a hard ceiling in seconds that this middleware will allow retries`retry_only_if_retry_after_header`booleanfalseRetry only if `RetryAfter` header sent`retry_on_status`array503, 429The response status codes that will trigger a retry`default_retry_multiplier`float or callable1.5Value to multiply the number of requests by if `RetryAfter` not supplied (see [below](#setting-default-retry-delay) for details)`on_retry_callback`callablenullOptional callback to call before a retry occurs`retry_on_timeout`booleanfalseSet to TRUE if you wish to retry requests that throw a ConnectException such as a timeout or 'connection refused'`expose_retry_header`booleanfalseSet to TRUE if you wish to expose the number of retries as a header on the response object`retry_header`stringX-Retry-CounterThe header key to use for the retry counter (if you need it)`retry_after_header`stringRetry-AfterThe remote server header key to look for information about how long to wait until retrying the request.`retry_after_date_format`string`D, d M Y H:i:s T`Optional customization for servers that return date/times that violate the HTTP spec`should_retry_callback`callablenullOptional callback to decide whether or not retry the request`retry_on_methods`string'\*' (all methods)Optional list of HTTP methods for which to run retries forEach option is discussed in detail below.

### Configuring Options

[](#configuring-options)

Options can be set in one of three places:

```
// Per request, in the same array as other Guzzle options
$response = $client->get('/some-url', [
   'max_retry_attempts' => 5,
   'on_retry_callback'  => $notifier
]);

// When you instantiate Guzzle, in the same array as other Guzzle options
$client = new \GuzzleHttp\Client([

    // Standard Guzzle options
    'base_url'        => 'https://example.org',
    'connect_timeout' => 10.0,

    // Retry options
    'max_retry_attempts' => 5,
    'on_retry_callback'  => $notifier
]);

// When you instantiate the Retry middleware
$stack = \GuzzleHttp\Stack::create();
$stack->push(GuzzleRetryMiddleware::factory([
    'max_retry_attempts' => 5,
    'on_retry_callback'  => $notifier
]));
```

If you specify options in two or more places, the configuration is merged as follows:

1. Individual request options take precedence over Guzzle constructor options
2. Guzzle constructor options take precedence over middleware constructor options.

### Setting maximum retry attempts

[](#setting-maximum-retry-attempts)

This value should be an integer equal to or greater than 0. Setting 0 or a negative effectively disables this middleware.

Setting this value to 0 is useful when you want to retry attempts by default, but disable retries for a particular request:

```
// Set the default retry attempts to 5
$client = new \GuzzleHttp\Client(['max_retry_attempts' => 5]);

// Do not retry this request
$client->get('/some/url', ['max_retry_attempts' => 0]);
```

### Setting status codes to retry

[](#setting-status-codes-to-retry)

By default, this middleware will retry requests when the server responds with a `429` or `503` HTTP status code. But, you can configure this:

```
$response = $client->get('/some-path', [
    'retry_on_status' => [429, 503, 500]
]);
```

If the response includes a `RetryAfter` header, but its status code is not in the list, it will not be processed.

**Note:** I haven't tested this, but I sincerely believe you will see some wonky behavior if you attempt to use this middleware with 3xx responses. I don't suggest it.

### Setting default retry delay

[](#setting-default-retry-delay)

If the response includes a valid `RetryAfter` header, this middleware will delay the next retry attempt the amount of time that the server specifies in that header.

If the response includes a *non-valid* `RetryAfter` or does not provide a `RetryAfter` header, then this middleware will use a default back-off algorithm: `multipler * number-of-attempts`:

Response with `RetryAfter` header:

```
      Client                 Server
      ------                 ------
      GET /resource    ->

                       get('/some-path', [
    'default_retry_multiplier' => 2.5
]);
```

You can also pass in a custom algorithm for setting the default delay timeout if you specify a callable for `default_retry_multiplier`:

```
// Custom callback to determine default timeout.  Note: $response may be NULL if a connect timeout occurred.
$response = $client->get('/some-path', [
    'default_retry_multiplier' => function($numRequests, ?ResponseInterface $response): float {
        return (float) rand(1, 5);
    }
]);
```

### Retrying requests that timeout

[](#retrying-requests-that-timeout)

You can configure this middleware to retry requests that timeout. Simply set the `retry_on_timeout` option to `true`:

```
// Retry this request if it times out:
$response = $client->get('/some-path', [
    'retry_on_timeout' => true,    // Set the retry middleware to retry when the connection or response times out
    'connect_timeout'  => 20,     // This is a built-in Guzzle option
    'timeout'          => 50      // This is also a built-in Guzzle option
]);

// You can also set these as defaults for every request:
$guzzle = new \GuzzleHttp\Client(['retry_on_timeout' => true, 'connect_timeout' => 20]);
$response = $guzzle->get('https://example.org');
```

As of version 2.12.0, this works for connection reset responses sent by the server (Curl error 104).

### On-Retry callback

[](#on-retry-callback)

You can supply a callback method that will be called before each time a request is retried. This is useful for logging, reporting, or anything else you can think of.

If you specify a callback, it will be called *before* the middleware calls the `usleep()` delay function.

The `request` and `options` arguments are sent by reference in case you want to modify them in the callback before the request is re-sent.

```
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Listen for retry events
 *
 * @param int                    $attemptNumber  How many attempts have been tried for this particular request
 * @param float                  $delay          How long the client will wait before retrying the request
 * @param RequestInterface       $request        Request
 * @param array                  $options        Guzzle request options
 * @param ResponseInterface|null $response       Response (or NULL if response not sent; e.g. connect timeout)
 * @param Throwable|null         $exception      This value will be present if the retry was triggered by onRejected
 *                                               (e.g. in the event of a connection timeout)
 */
$listener = function(
    int $attemptNumber,
    float $delay,
    RequestInterface &$request,
    array &$options,
    ?ResponseInterface $response,
    ?Throwable $exception
) {

    echo sprintf(
        "Retrying request to %s.  Server responded with %s.  Will wait %s seconds.  This is attempt #%s. The error was %s",
        $request->getUri()->getPath(),
        $response->getStatusCode(),
        number_format($delay, 2),
        $attemptNumber,
        $exception->getMessage()
    );
}

$client = new \GuzzleHttp\Client([
    'on_retry_callback' => $listener
]);

$response = $client->get('/some/path');
```

### Enabling or disabling per-request

[](#enabling-or-disabling-per-request)

Suppose that you have setup default retry options as follows:

```
$stack = \GuzzleHttp\Stack::create();
$stack->push(GuzzleRetryMiddleware::factory(['max_retry_attempts' => 5]));
$client = new \GuzzleHttp\Client(['handler' => $stack]);
```

You can disable retry for individual requests as by setting the `retry_enabled` parameter in the request options:

```
// Retry will NOT be attempted for this request..
$client->get('https://example.org', ['retry_enabled' => false]);

// Retry WILL be attempted for this request...
$client->get('https://example.org');
```

### Adding a custom retry header to HTTP responses

[](#adding-a-custom-retry-header-to-http-responses)

Sometimes for debugging purposes, it is useful to know how many times a request was retried when getting a response. For this purpose, this library can add a custom header to responses; simply set the `expose_retry_header` option to `TRUE`.

*Note*: This modifies the HTTP response on the client. If you don't want to alter the response retrieved from the server, you can also use [callbacks](#on-retry-callback) to get the request count.

Example:

```
// Retry this request if it times out:
$response = $client->get('/some-path', [
    'expose_retry_header' => true  // This adds the 'X-Retry-Counter' if a request was retried
]);

// If a request was retried, the response will include the 'X-Retry-Counter'
$numRetries = (int) $response->getHeaderLine('X-Retry-Counter');
```

You can also specify a custom header key:

```
// Retry this request if it times out:
$response = $client->get('/some-path', [
    'expose_retry_header' => true,
    'retry_header'        => 'X-Retry-Count'
]);

// If a request was retried, the response will include the 'X-Retry-Counter'
$numRetries = (int) $response->getHeaderLine('X-Retry-Count');
```

### Modifying the expected header name from `Retry-After`

[](#modifying-the-expected-header-name-from-retry-after)

You can change the header that the client expects the server to respond with. By default, the client looks for the `Retry-After` header, but in some edge-cases, servers may choose to respond with a different header.

```
// Change the name of the expected retry after header to something else:
$response = $client->get('/some-path', [
    'retry_after_header' => 'X-Custom-Retry-After-Seconds'
]);

// Otherwise, the default `Retry-After` header will be used.
$response = $client->get('/some-path');
```

### Setting a custom date format for the `Retry-After` header

[](#setting-a-custom-date-format-for-the-retry-after-header)

You can change the expected date format expected from the server that the client library expects. By default, this library expects an RFC 2822 header as defined in the [HTTP spec](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date). In certain edge-cases, the server may implement some other date format. This library allows for the possibility of that.

*Note*: Be careful not to use this option with the Unix epoch (`u`) format. The client will interpret this value as an integer and subsequently timeout
for a very, very long time.

### Setting a maximum allowable timeout value

[](#setting-a-maximum-allowable-timeout-value)

If you want the client to not accept timeout values greater than a certain value, set the `max_allowable_timeout_secs` option. This will return a static number once the timeout reaches a specified length regardless if it is calculated using the default backoff algorithm or returned from the server via the `Retry-After` header.

By default, this value is `null`, which means there is no limit.

```
// Set the maximum allowable timeout
// If the calculated value exceeds 120 seconds, then just return 120 seconds
$response = $client->get('/some-path', [
    'max_allowable_timeout_secs' => 120
]);
```

### Setting a hard time ceiling for all retries

[](#setting-a-hard-time-ceiling-for-all-retries)

If you want to set a hard time-limit for all retry requests, set the `give_up_after_secs` option. If set, this will be checked before the number of retries is, so any requests will fail even if you haven't reached your retry count limit.

```
// This will fail when either the number of seconds is reached, or the number of retry attempts is reached, whichever
// comes first
$response = $client->get('/some-path', [
    'max_retry_attempts' => 10
    'give_up_after_secs' => 10
]);
```

### Setting specific HTTP methods to retry on

[](#setting-specific-http-methods-to-retry-on)

By default, this library retries all request methods (`GET`, `POST`, `PATCH`, etc...). If you want to limit the HTTP methods that this library will retry requests for, specify the `retry_on_methods` option with an array of methods:

```
//
$response = $client->get('/some-path', [
    'retry_on_methods' => ['GET', 'OPTIONS', 'HEAD']
]);
```

### Custom retry decision logic

[](#custom-retry-decision-logic)

Occasionally, servers will fail to provide an appropriate HTTP error code when a response needs to be retried. For example, consider a server that returns a `200` status code, but with a message body instructing you to wait. In cases like this, you can use the `should_retry_callback` option to implement a callback method that returns `true` (should retry) or `false`(should not retry).

Another use for this callback could be based on the request itself. For example, you may only want to retry requests that have a specific header set.

It's important to note that the callback will be called only if all the following circumstances are true:

- The `retry_enabled` option is `true` (default: `true`)
- The number of retries has not exceeded the value set in `max_retry_attempts` (default: `10`)
- The total time elapsed is less than the `give_up_after_secs` value (default: *disabled*)

```
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$callback = function (array $options, ?ResponseInterface $response, RequestInterface $request): bool {
    // Only allow retries if x-header header is set
    if(! $request->hasHeader('x-header')) {
        return false;
    }

    // Response will be NULL in the event of a connection timeout, so your callback function
    // will need to be able to handle that case
    if (! $response) {
        return true;
    }

    // Get the HTTP body as a string
    $body = (string) $response->getBody();
    return str_contains($body, 'error'); // NOTE: The str_contains function is available only in PHP 8+
};

$response = $client->get('/some-path', [
    // ..other options..,
   'should_retry_callback' => $callback
]);
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

*Note:* Since this library tests timeouts, a few of the tests take a 2-3 seconds to run.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Casey McLaughlin](https://github.com/caseyamcl)
- [Jachim Coudenys](https://github.com/coudenysj)
- [Markus Podar](https://github.com/mfn)
- [All contributors](https://github.com/caseyamcl/guzzle_retry_middleware/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance54

Moderate activity, may be stable

Popularity65

Solid adoption and visibility

Community39

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 86.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~145 days

Recently: every ~96 days

Total

21

Last Release

312d ago

Major Versions

v1.0 → v2.02017-10-02

PHP version history (3 changes)v1.0PHP ~5.5|~7.0

v2.3PHP ^7.1

v2.5PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/8db51cd614310a5de4822be9602de578e6d6dd2af7949d52fcd375ba2a8d1c74?d=identicon)[caseyamcl](/maintainers/caseyamcl)

---

Top Contributors

[![caseyamcl](https://avatars.githubusercontent.com/u/53035?v=4)](https://github.com/caseyamcl "caseyamcl (163 commits)")[![teandr](https://avatars.githubusercontent.com/u/4022382?v=4)](https://github.com/teandr "teandr (4 commits)")[![jamesaspence](https://avatars.githubusercontent.com/u/6488043?v=4)](https://github.com/jamesaspence "jamesaspence (3 commits)")[![andrewdalpino](https://avatars.githubusercontent.com/u/18690561?v=4)](https://github.com/andrewdalpino "andrewdalpino (3 commits)")[![alexeyshockov](https://avatars.githubusercontent.com/u/203120?v=4)](https://github.com/alexeyshockov "alexeyshockov (2 commits)")[![Krunch](https://avatars.githubusercontent.com/u/1214548?v=4)](https://github.com/Krunch "Krunch (2 commits)")[![danmichaelo](https://avatars.githubusercontent.com/u/434495?v=4)](https://github.com/danmichaelo "danmichaelo (2 commits)")[![Illizian](https://avatars.githubusercontent.com/u/703800?v=4)](https://github.com/Illizian "Illizian (1 commits)")[![mfn](https://avatars.githubusercontent.com/u/87493?v=4)](https://github.com/mfn "mfn (1 commits)")[![nexxai](https://avatars.githubusercontent.com/u/4316564?v=4)](https://github.com/nexxai "nexxai (1 commits)")[![Nicolai-](https://avatars.githubusercontent.com/u/2822977?v=4)](https://github.com/Nicolai- "Nicolai- (1 commits)")[![rkok](https://avatars.githubusercontent.com/u/6299130?v=4)](https://github.com/rkok "rkok (1 commits)")[![sunaoka](https://avatars.githubusercontent.com/u/105845?v=4)](https://github.com/sunaoka "sunaoka (1 commits)")[![Tarasovych](https://avatars.githubusercontent.com/u/24782123?v=4)](https://github.com/Tarasovych "Tarasovych (1 commits)")[![coudenysj](https://avatars.githubusercontent.com/u/96260?v=4)](https://github.com/coudenysj "coudenysj (1 commits)")[![ederuiter](https://avatars.githubusercontent.com/u/6728434?v=4)](https://github.com/ederuiter "ederuiter (1 commits)")[![fredericgboutin-yapla](https://avatars.githubusercontent.com/u/91906601?v=4)](https://github.com/fredericgboutin-yapla "fredericgboutin-yapla (1 commits)")

---

Tags

guzzleguzzle-middlewarehttpmiddlewarephpretrymiddlewareGuzzleretrycaseyamclguzzle\_retry\_middlewareback-offretry-after

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/caseyamcl-guzzle-retry-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/caseyamcl-guzzle-retry-middleware/health.svg)](https://phpackages.com/packages/caseyamcl-guzzle-retry-middleware)
```

###  Alternatives

[kevinrob/guzzle-cache-middleware

A HTTP/1.1 Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack. (RFC 7234)

43417.4M104](/packages/kevinrob-guzzle-cache-middleware)[rtheunissen/guzzle-log-middleware

Guzzle middleware to log requests and responses

842.3M17](/packages/rtheunissen-guzzle-log-middleware)[eljam/guzzle-jwt-middleware

A jwt authentication middleware for guzzle 6

28722.5k3](/packages/eljam-guzzle-jwt-middleware)[rtheunissen/guzzle-rate-limiter

Guzzle 6 middleware used to delay requests dynamically

52177.2k1](/packages/rtheunissen-guzzle-rate-limiter)[brightfish/caching-guzzle

Cache HTTP responses through Guzzle middleware

1031.5k](/packages/brightfish-caching-guzzle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
