[HttpClient] Add doc for multiple base_uri as array#18227
[HttpClient] Add doc for multiple base_uri as array#18227OskarStark merged 1 commit intosymfony:6.3from
base_uri as array#18227Conversation
|
Is the failed build normal? It doesn't seem related to my changes, and seems to affect other PRs towards 6.3 but I'm not sure. |
|
A I think we can make this a bit shorter and more straight to the point: The ``RetryableHttpClient`` can be configured to use multiple base URIs. This feature provides increased flexibility and reliability for making HTTP requests. Pass an array of base URIs as option ``base_uri`` when making a request:
.. code-block:: php
$response = $client->request('GET', 'foo-bar', [
'base_uri' => [
'http://example.com/a/', // first request will use this base URI
'http://example.com/b/', // if first request fails, this second base URI will be used
],
]);
If you want to shuffle the order of base URIs for each retry attempt, nest the base URIs you want to shuffle in an additional array:
.. code-block:: php
$response = $client->request('GET', 'foo-bar', [
'base_uri' => [
'http://example.com/a/',
[
'http://example.com/b/', // one random URI from this list will be used on retry #2
'http://example.com/c/',
],
'http://example.com/d/', // non-nested base URIs are used in order
],
]);
This feature allows for a more randomized approach to handling retries, reducing the likelihood of repeatedly hitting the same failed base URI.
By using a nested array for the first base URI, you can use this feature to distribute the load among many nodes in a cluster of servers.
When the number of retries is higher than the number of base URIs, the last base URI will be used for remaining retries.
You can also configure the array of base URIs using the ``withOptions()`` method:
.. code-block:: php
$client = $client->withOptions(['base_uri' => [
'http://example.com/a/',
'http://example.com/b/',
]]); |
Yeah, indeed, sorry! Should I put the entire block inside the
Ok, wil change this during break! |
|
Just curious about one thing:
It's not just for the first base URI unless I misunderstood one of your modifications. Or did I miss something? |
retries should be rare, so that randomizing only the second base URI wouldn't provide load balancing |
Okay got it, thanks! Was just to be sure though. Technically speaking, it works on any key of the array but it should be extremely rare to use it on subsequent keys indeed. |
ba34ef2 to
56d310e
Compare
There was a problem hiding this comment.
Merge remote-tracking
never merge, always rebase
Okay sorry, going back for a rebase! |
56d310e to
83ec702
Compare
base_uri as array
ca1b333 to
b1c1a84
Compare
b1c1a84 to
7244375
Compare
|
Thanks Benjamin for working on this feature, this is much appreciated and congratulations on your first contribution to the Symfony documentation 🎉 |
Add documentation for multiple base_uris in HttpClient (Symfony PR (merged): symfony/symfony#49809 )
Fixes #18224