Httpclient Retry #486
Replies: 1 comment 5 replies
-
Hey @arunnabraham, thanks for bringing this up 👍 You can achieve a retry mechanism using a similar example as this one: <?php
require __DIR__ . '/vendor/autoload.php';
$client = new React\Http\Browser();
$promise = $client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
// promise fullfilled
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
// promise rejected
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}); This examples shows a simple promise API. We try to send a request to "google.com/" and then define what should happen if it was successful (promise fulfilled) and what happens if something goes wrong (promise rejected). In our successful case we would just var_dump the response headers and body. If an error occurs, we would just show the error message. You can read more about promises in our documentation: https://github.com/reactphp/http#promises We can now come back to your retry mechanism, which means we want to try again if something goes wrong, so let's change this example a bit: <?php
require __DIR__ . '/vendor/autoload.php';
$client = new React\Http\Browser();
$promise = $client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function () {
// retry logic
}); This example just uses the rejected part for the retry logic. There are still some things you have to take into consideration e.g. how often do you retry. Be aware that this $result = React\Async\await($promise); We're also currently working on v3 for ReactPHP with the goal to make everything easier, so there will be some simpler implementations in the future. I hope this helps 👍 |
Beta Was this translation helpful? Give feedback.
-
Hello team I need help/advice regarding HTTP retry mechanism in the HTTP client, I tried in a way to create a retry, but the results coming are not stable. (I am quite new to working on async) . I would like to see a simpler implementation of this. Please let me know.
Beta Was this translation helpful? Give feedback.
All reactions