Retrying behavior for Guzzle
Add dependency mikeevstropov/guzzle
$ composer require mikeevstropov/guzzle
-
requests_limit
Accepts:
integer
- positive integer
Default:
1
- only one attempt
Maximum number of attempts to receive a response.
-
repeat_on
Accepts:
array
- array with a numeric index
Default:
array(0, 5)
- repeat failed request or if response code is 5xx
List of error codes for retrying requests:
array(5)
- onGuzzleHttp\Exception\ServerException
(5xx code)array(4)
- onGuzzleHttp\Exception\ClientException
(4xx code)array(0)
- otherTransferException
likeGuzzleHttp\Exception\ConnectException
You can combine it like
array(4, 5).
<?php
$client = new \GuzzleHttp\Client();
// Let's try to request "http://httpstat.us/503" that
// page will always return "503 Service Unavailable"
$response = $client->get('http://httpstat.us/503', [
'requests_limit' => 3,
]); // will thrown GuzzleHttp\Exception\ServerException after 3 attempts
// We can pass option "repeat_on" to prevent retrying
// if response has code 5xx (by default [0, 5])
$response = $client->get('http://httpstat.us/503', [
'requests_limit' => 3,
'repeat_on' => array(0, 4)
]); // will thrown GuzzleHttp\Exception\ServerException after first request
// But same options with request to the page that return 4xx
// will have 3 attempts, because we pass "4" as array item in
// option "repeat_on"
$response = $client->get('http://httpstat.us/402', [
'requests_limit' => 3,
'repeat_on' => array(0, 4)
]); // will thrown GuzzleHttp\Exception\ServerException after 3 attempts
Clone
$ git clone https://github.com/mikeevstropov/guzzle.git
Go to project
$ cd guzzle
Install dependencies
$ composer install
Increase composer timeout. Since composer by default set it to 300 seconds.
$ composer config --global process-timeout 600
Run the tests
$ composer test