Skip to content

Commit cb3a7b8

Browse files
committed
Add documentation for Retryable client
1 parent ba42b5a commit cb3a7b8

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

http_client.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Some options are described in this guide:
143143
* `Query String Parameters`_
144144
* `Headers`_
145145
* `Redirects`_
146+
* `Retry Failed Requests`_
146147
* `HTTP Proxies`_
147148

148149
Check out the full :ref:`http_client config reference <reference-http-client>`
@@ -654,6 +655,26 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
654655
'max_redirects' => 0,
655656
]);
656657

658+
Retry Failed Requests
659+
~~~~~~~~~~~~~~~~~~~~~
660+
661+
Some times, requests failed because of temporary issue in the server or
662+
because network issue. You can use the
663+
:class:`Symfony\\Component\\HttpClient\\RetryableHttpClient`
664+
client to automatically retry the request when it failed.::
665+
666+
$client = new RetryableHttpClient(HttpClient::create());
667+
668+
The ``RetryableHttpClient`` uses a
669+
:class:`Symfony\\Component\\HttpClient\\Retry\\RetryDeciderInterface` to
670+
decide if the request should be retried, and a
671+
:class:`Symfony\\Component\\HttpClient\\Retry\\RetryBackOffInterface` to
672+
define the waiting time between each request.
673+
674+
By default, it retries until 3 attemps, the requests responding with a
675+
status code in (423, 425, 429, 500, 502, 503, 504, 507 or 510) and wait
676+
expentially from 1 second for the first retry, to 4 seconds at the 3rd attempt.
677+
657678
HTTP Proxies
658679
~~~~~~~~~~~~
659680

reference/configuration/framework.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,34 @@ Configuration
137137
* `proxy`_
138138
* `query`_
139139
* `resolve`_
140+
141+
* :ref:`retry_failed <reference-http-client-retry-failed>`
142+
143+
* `backoff_service`_
144+
* `decider_service`_
145+
* :ref:`enabled <reference-http-client-retry-enabled>`
146+
* `delay`_
147+
* `http_codes`_
148+
* `max_delay`_
149+
* `max_retries`_
150+
* `multiplier`_
151+
140152
* `timeout`_
141153
* `max_duration`_
142154
* `verify_host`_
143155
* `verify_peer`_
144156

157+
* :ref:`retry_failed <reference-http-client-retry-failed>`
158+
159+
* `backoff_service`_
160+
* `decider_service`_
161+
* :ref:`enabled <reference-http-client-retry-enabled>`
162+
* `delay`_
163+
* `http_codes`_
164+
* `max_delay`_
165+
* `max_retries`_
166+
* `multiplier`_
167+
145168
* `http_method_override`_
146169
* `ide`_
147170
* :ref:`lock <reference-lock>`
@@ -742,6 +765,33 @@ If you use for example
742765
as the type and name of an argument, autowiring will inject the ``my_api.client``
743766
service into your autowired classes.
744767

768+
.. _reference-http-client-retry-failed:
769+
770+
By enabling the optional ``retry_failed`` configuration, the HTTP client service
771+
will automaticaly retry failed HTTP requests.
772+
773+
.. code-block:: yaml
774+
775+
# config/packages/framework.yaml
776+
framework:
777+
# ...
778+
http_client:
779+
# ...
780+
retry_failed:
781+
# backoff_service: app.custom_backoff
782+
# decider_service: app.custom_decider
783+
http_codes: [429, 500]
784+
max_retries: 2
785+
delay: 1000
786+
multiplier: 3
787+
max_delay: 500
788+
789+
scoped_clients:
790+
my_api.client:
791+
# ...
792+
retry_failed:
793+
max_retries: 4
794+
745795
auth_basic
746796
..........
747797

@@ -769,6 +819,19 @@ in the `Microsoft NTLM authentication protocol`_. The value of this option must
769819
follow the format ``username:password``. This authentication mechanism requires
770820
using the cURL-based transport.
771821

822+
backoff_service
823+
...............
824+
825+
**type**: ``string``
826+
827+
The service id used to compute the time to wait between retries. By default, it
828+
uses an instance of
829+
:class:`Symfony\\Component\\HttpClient\\Retry\\ExponentialBackOff` configured
830+
with ``delay``, ``max_delay`` and ``multiplier`` options. This class has to
831+
implement :class:`Symfony\\Component\\HttpClient\\Retry\\RetryBackOffInterface`.
832+
This options cannot be used along `delay`_, `max_delay`_ or `multiplier`_
833+
options.
834+
772835
base_uri
773836
........
774837

@@ -837,6 +900,36 @@ ciphers
837900
A list of the names of the ciphers allowed for the SSL/TLS connections. They
838901
can be separated by colons, commas or spaces (e.g. ``'RC4-SHA:TLS13-AES-128-GCM-SHA256'``).
839902

903+
decider_service
904+
...............
905+
906+
**type**: ``string``
907+
908+
The service id used to decide if a request should be retried. By default, it
909+
uses an instance of
910+
:class:`Symfony\\Component\\HttpClient\\Retry\\HttpStatusCodeDecider` configured
911+
with ``http_codes`` options. This class has to
912+
implement :class:`Symfony\\Component\\HttpClient\\Retry\\RetryDeciderInterface`.
913+
This options cannot be used along `http_codes`_ option.
914+
915+
delay
916+
.....
917+
918+
**type**: ``integer`` **default**: ``1000``
919+
920+
The initial delay in milliseconds used to compute the waiting time between
921+
retries. This options cannot be used along `backoff_service`_ option.
922+
923+
.. _reference-http-client-retry-enabled:
924+
925+
enabled
926+
.......
927+
928+
**type**: ``boolean`` **default**: ``false``
929+
930+
Whether to enable the support for retry failed HTTP request or not.
931+
This setting is automatically set to true when one of the child settings is configured.
932+
840933
headers
841934
.......
842935

@@ -845,6 +938,14 @@ headers
845938
An associative array of the HTTP headers added before making the request. This
846939
value must use the format ``['header-name' => header-value, ...]``.
847940

941+
http_codes
942+
..........
943+
944+
**type**: ``array`` **default**: ``[423, 425, 429, 500, 502, 503, 504, 507, 510]``
945+
946+
The list of HTTP status codes that triggers a retry of the request.
947+
This options cannot be used along `decider_service`_ option.
948+
848949
http_version
849950
............
850951

@@ -870,6 +971,15 @@ local_pk
870971
The path of a file that contains the `PEM formatted`_ private key of the
871972
certificate defined in the ``local_cert`` option.
872973

974+
max_delay
975+
.........
976+
977+
**type**: ``integer`` **default**: ``0``
978+
979+
The maximum amount of milliseconds initial to wait between retries.
980+
Use ``0`` to not limit the duration.
981+
This options cannot be used along `backoff_service`_ option.
982+
873983
max_duration
874984
............
875985

@@ -896,6 +1006,22 @@ max_redirects
8961006
The maximum number of redirects to follow. Use ``0`` to not follow any
8971007
redirection.
8981008

1009+
max_retry
1010+
.........
1011+
1012+
**type**: ``integer`` **default**: ``3``
1013+
1014+
The maximum number of retries before aborting. When the maximum is reach, the
1015+
client returns the last received responses.
1016+
1017+
multiplier
1018+
..........
1019+
1020+
**type**: ``float`` **default**: ``2``
1021+
1022+
Multiplier to apply to the delay each time a retry occurs.
1023+
This options cannot be used along `backoff_service`_ option.
1024+
8991025
no_proxy
9001026
........
9011027

0 commit comments

Comments
 (0)