Skip to content

improve retry documentation and add all options #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 6, 2019
Merged
65 changes: 48 additions & 17 deletions plugins/retry.rst
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
Retry Plugin
============

The ``RetryPlugin`` can automatically attempt to re-send a request that failed, to work around
unreliable connections and servers. It relies on errors to throw exceptions, so you need to
place something like the :doc:`ErrorPlugin <error>` later in the plugin chain::
The ``RetryPlugin`` can automatically attempt to re-send a request that failed,
to work around unreliable connections and servers. It re-sends the request when
an exception is thrown, unless the exception is a HttpException for a status
code in the 5xx server error range. Since version 2.0, responses with status
codes in the 5xx range are also retried. Each retry attempt is delayed by an
exponential backoff time.

use Http\Discovery\HttpClientDiscovery;
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\Plugin\RetryPlugin;

$pluginClient = new PluginClient(
HttpClientDiscovery::find(),
[
new RetryPlugin(),
new ErrorPlugin(),
]
);
See below for how to configure that behaviour.

.. warning::

Expand All @@ -30,12 +22,51 @@ but simply tries again from the current position.
Async
-----

This plugin is not fully compatible with asynchronous behavior, as the wait between retries is done
with a blocking call to a sleep function.
This plugin is not fully compatible with asynchronous behavior, as the wait
between retries is done with a blocking call to a sleep function.

Options
-------

``retries``: int (default: 1)

Number of retry attempts to make before giving up.

``error_response_decider``: callable (default: retry if status code is in 5xx range)

A callback function that gets the request and response to decide whether the
request should be retried.

``exception_decider``: callable (default: retry if not a HttpException or status code is in 5xx range)

A callback function that gets a request and an exception to decide after a
failure whether the request should be retried.

``error_response_delay``: callable (default: exponential backoff)

A callback that gets a request and response and the current number of retries
and returns how many microseconds we should wait before trying again.

``exception_delay``: callable (default: exponential backoff)

A callback that gets a request, an exception and the current number of retries
and returns how many microseconds we should wait before trying again.

Interaction with Exceptions
---------------------------

If you use the :doc:`ErrorPlugin <error>`, you should place it later in the
plugin chain::

use Http\Discovery\HttpClientDiscovery;
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\Plugin\RetryPlugin;

$pluginClient = new PluginClient(
HttpClientDiscovery::find(),
[
new RetryPlugin(),
new ErrorPlugin(),
]
);