@@ -734,58 +734,59 @@ each retry.
734
734
Retry over different uris
735
735
.........................
736
736
737
- When using a ``RetryableHttpClient ``, the ``base_uri `` can be set as an array of
738
- uris. The array will be used in the given order, meaning that the first request
739
- will be made with the first uri in the array. Should it fail, the next one will
740
- be used for the first retry. If the number of configured retries is greater than
741
- the number of given uris, the last uri will be used for every exceding retry.
737
+ The ``RetryableHttpClient `` can be configured to use multiple base URIs. This
738
+ feature provides increased flexibility and reliability for making HTTP
739
+ requests. Pass an array of base URIs as option ``base_uri `` when making a
740
+ request:
742
741
743
- You can pass the array of base uris directly to the ``request `` method or when
744
- creating a HttpClient with the ``withOptions `` method::
745
-
746
- use Symfony\Component\HttpClient\RetryableHttpClient;
747
-
748
- $client = new RetryableHttpClient(HttpClient::create());
742
+ .. code-block :: php
749
743
750
- $response = $client->request('GET', 'some-path ', [
744
+ $response = $client->request('GET', 'foo-bar ', [
751
745
'base_uri' => [
752
- // first request will use this base uri
746
+ // first request will use this base URI
753
747
'http://example.com/a/',
754
- // if first request fails, the second base uri will be used
748
+ // if first request fails, this second base URI will be used
755
749
'http://example.com/b/',
756
750
],
757
751
]);
758
752
759
- // You can also use the withOptions method to pass an array of base_uri
760
- $client = $client->withOptions(['base_uri' => [
761
- 'http://example.com/a/',
762
- 'http://example.com/b/',
763
- ]]);
764
-
765
- $response = $client->request('GET', 'some-path');
766
-
767
- If you wish some or all of the base uris to be shuffled, you can pass a nested
768
- array inside the ``base_uri `` array. Every uri inside the nested array will be
769
- randomized, and the order of the base uris outside this nested array will be
770
- preserved::
753
+ When the number of retries is higher than the number of base URIs, the
754
+ last base URI will be used for remaining retries.
771
755
772
- use Symfony\Component\HttpClient\RetryableHttpClient;
756
+ If you want to shuffle the order of base URIs for each retry attempt, nest the
757
+ base URIs you want to shuffle in an additional array:
773
758
774
- $client = new RetryableHttpClient(HttpClient::create());
759
+ .. code-block :: php
775
760
776
- $response = $client->request('GET', 'some-path ', [
761
+ $response = $client->request('GET', 'foo-bar ', [
777
762
'base_uri' => [
778
763
'http://example.com/a/',
779
764
[
780
- // base uri passed inside a nested array will be shuffled
765
+ // one random URI from this list will be used on retry #2
781
766
'http://example.com/b/',
782
767
'http://example.com/c/',
783
768
],
784
- // the order of the non-nested base uris is preserved
769
+ // non-nested base URIs are used in order
785
770
'http://example.com/d/',
786
771
],
787
772
]);
788
773
774
+ This feature allows for a more randomized approach to handling retries,
775
+ reducing the likelihood of repeatedly hitting the same failed base URI.
776
+
777
+ By using a nested array for the base URI, you can use this feature
778
+ to distribute the load among many nodes in a cluster of servers.
779
+
780
+ You can also configure the array of base URIs using the ``withOptions() ``
781
+ method:
782
+
783
+ .. code-block :: php
784
+
785
+ $client = $client->withOptions(['base_uri' => [
786
+ 'http://example.com/a/',
787
+ 'http://example.com/b/',
788
+ ]]);
789
+
789
790
HTTP Proxies
790
791
~~~~~~~~~~~~
791
792
0 commit comments