What's the best way to retry after request timeout or get some other error code when I use the newest guzzle? https://github.com/guzzle/guzzle/issues/1806
require './vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
class TestRetry {
public function test()
{
$handlerStack = HandlerStack::create(new CurlHandler());
$handlerStack->push(Middleware::retry($this->retryDecider(), $this->retryDelay()));
$client = new Client(array('handler' => $handlerStack));
$response = $client->request(
'GET',
// @todo replace to a real url!!!
'https://500-error-code-url'
)->getBody()->getContents();
return \GuzzleHttp\json_decode($response, true);
}
public function retryDecider()
{
return function (
$retries,
Request $request,
Response $response = null,
RequestException $exception = null
) {
// Limit the number of retries to 5
if ($retries >= 5) {
return false;
}
// Retry connection exceptions
if ($exception instanceof ConnectException) {
return true;
}
if ($response) {
// Retry on server errors
if ($response->getStatusCode() >= 500 ) {
return true;
}
}
return false;
};
}
/**
* delay 1s 2s 3s 4s 5s
*
* @return Closure
*/
public function retryDelay()
{
return function ($numberOfRetries) {
return 1000 * $numberOfRetries;
};
}
}
$TestRetry = new TestRetry();
$TestRetry->test();