рекурсивная отправка запроса в случае timeout, количество попыток - 3
This commit is contained in:
parent
1cb4074470
commit
6ee6452748
@ -15,7 +15,7 @@ class Client
|
|||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $defaultParameters;
|
protected $defaultParameters;
|
||||||
protected $retry;
|
public $retry;
|
||||||
|
|
||||||
public function __construct($url, array $defaultParameters = array())
|
public function __construct($url, array $defaultParameters = array())
|
||||||
{
|
{
|
||||||
@ -58,22 +58,27 @@ class Client
|
|||||||
|
|
||||||
$parameters = array_merge($this->defaultParameters, $parameters);
|
$parameters = array_merge($this->defaultParameters, $parameters);
|
||||||
|
|
||||||
$path = $this->url . $path;
|
$url = $this->url . $path;
|
||||||
|
|
||||||
if (self::METHOD_GET === $method && sizeof($parameters)) {
|
if (self::METHOD_GET === $method && sizeof($parameters)) {
|
||||||
$path .= '?' . http_build_query($parameters);
|
$url .= '?' . http_build_query($parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $path);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||||
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $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) {
|
if (self::METHOD_POST === $method) {
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
|
||||||
@ -86,6 +91,20 @@ class Client
|
|||||||
|
|
||||||
curl_close($ch);
|
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) {
|
if ($errno) {
|
||||||
throw new CurlException($error, $errno);
|
throw new CurlException($error, $errno);
|
||||||
}
|
}
|
||||||
|
@ -61,11 +61,11 @@ class ClientTest extends TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @group integration
|
* @group integration
|
||||||
* @expectedException \RetailCrm\Exception\CurlException
|
|
||||||
*/
|
*/
|
||||||
public function testMakeRequestTimeout()
|
public function testMakeRequestRepeatOnTimeout()
|
||||||
{
|
{
|
||||||
$client = static::getClient();
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user