1
0
mirror of synced 2024-11-22 05:16:07 +03:00

рекурсивная отправка запроса в случае timeout, количество попыток - 3

This commit is contained in:
Alex Lushpai 2015-06-15 17:50:31 +03:00
parent 1cb4074470
commit 6ee6452748
2 changed files with 28 additions and 9 deletions

View File

@ -15,7 +15,7 @@ class Client
protected $url;
protected $defaultParameters;
protected $retry;
public $retry;
public function __construct($url, array $defaultParameters = array())
{
@ -58,22 +58,27 @@ class Client
$parameters = array_merge($this->defaultParameters, $parameters);
$path = $this->url . $path;
$url = $this->url . $path;
if (self::METHOD_GET === $method && sizeof($parameters)) {
$path .= '?' . http_build_query($parameters);
$url .= '?' . http_build_query($parameters);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify);
if (!$debug) {
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT_MS, (int) $timeout + ($this->retry * 2000));
}
if (self::METHOD_POST === $method) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
@ -86,6 +91,20 @@ class Client
curl_close($ch);
if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) {
$errno = null;
$error = null;
$this->retry += 1;
$this->makeRequest(
$path,
$method,
$parameters,
$timeout,
$verify,
$debug
);
}
if ($errno) {
throw new CurlException($error, $errno);
}

View File

@ -61,11 +61,11 @@ class ClientTest extends TestCase
/**
* @group integration
* @expectedException \RetailCrm\Exception\CurlException
*/
public function testMakeRequestTimeout()
public function testMakeRequestRepeatOnTimeout()
{
$client = static::getClient();
$client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true);
$response = $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true);
$this->assertGreaterThanOrEqual(1, $client->retry);
}
}