Allow setRetry() methods to use callables

This commit is contained in:
Zach Borboa
2017-09-02 02:23:40 -07:00
parent 0f1e16a6fa
commit 4ec89be9bc
5 changed files with 219 additions and 27 deletions
+48 -16
View File
@@ -45,7 +45,7 @@ class Curl
public $retries = 0;
public $isChildOfMultiCurl = false;
public $remainingRetries = 0;
private $maximumNumberOfRetries = 0;
public $retryDecider = null;
private $cookies = array();
private $headers = array();
@@ -390,18 +390,22 @@ class Curl
}
$this->errorMessage = $this->curlError ? $this->curlErrorMessage : $this->httpErrorMessage;
if ($this->error && $this->remainingRetries >= 1) {
$this->retries += 1;
// Allow multicurl to attempt retry as needed.
if ($this->isChildOfMultiCurl) {
return;
}
// Allow multicurl to update $remainingRetries and retry.
if ($this->isChildOfMultiCurl) {
return;
}
$this->remainingRetries -= 1;
if ($this->attemptRetry()) {
return $this->exec($ch);
}
$this->execDone();
return $this->response;
}
public function execDone()
{
if ($this->error) {
$this->call($this->errorFunction);
} else {
@@ -414,8 +418,6 @@ class Curl
if (!($this->fileHandle === null)) {
$this->downloadComplete($this->fileHandle);
}
return $this->response;
}
/**
@@ -1033,15 +1035,20 @@ class Curl
/**
* Set Retry
*
* Number of retries to attempt. Maximum number of attempts is $maximum_number_of_retries + 1.
* Number of retries to attempt or decider callable. Maximum number of
* attempts is $maximum_number_of_retries + 1.
*
* @access public
* @param $maximum_number_of_retries
* @param $mixed
*/
public function setRetry($maximum_number_of_retries = 0)
public function setRetry($mixed)
{
$this->maximumNumberOfRetries = $maximum_number_of_retries;
$this->remainingRetries = $this->maximumNumberOfRetries;
if (is_callable($mixed)) {
$this->retryDecider = $mixed;
} elseif (is_int($mixed)) {
$maximum_number_of_retries = $mixed;
$this->remainingRetries = $maximum_number_of_retries;
}
}
/**
@@ -1080,6 +1087,31 @@ class Curl
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
}
/**
* Attempt Retry
*
* @access public
*/
public function attemptRetry()
{
$attempt_retry = false;
if ($this->error) {
if ($this->retryDecider === null) {
$attempt_retry = $this->remainingRetries >= 1;
} else {
$func = $this->retryDecider;
$attempt_retry = $func($this);
}
if ($attempt_retry) {
$this->retries += 1;
if ($this->remainingRetries) {
$this->remainingRetries -= 1;
}
}
}
return $attempt_retry;
}
/**
* Success
*
+10 -9
View File
@@ -18,7 +18,7 @@ class MultiCurl
private $errorFunction = null;
private $completeFunction = null;
private $maximumNumberOfRetries = 0;
private $retry = null;
private $cookies = array();
private $headers = array();
@@ -592,14 +592,15 @@ class MultiCurl
/**
* Set Retry
*
* Number of retries to attempt. Maximum number of attempts is $maximum_number_of_retries + 1.
* Number of retries to attempt or decider callable. Maximum number of
* attempts is $maximum_number_of_retries + 1.
*
* @access public
* @param $maximum_number_of_retries
* @param $mixed
*/
public function setRetry($maximum_number_of_retries = 0)
public function setRetry($mixed)
{
$this->maximumNumberOfRetries = $maximum_number_of_retries;
$this->retry = $mixed;
}
/**
@@ -676,9 +677,7 @@ class MultiCurl
$ch->curlErrorCode = $info_array['result'];
$ch->exec($ch->curl);
if ($ch->error && $ch->remainingRetries >= 1) {
$ch->remainingRetries -= 1;
if ($ch->attemptRetry()) {
// Remove completed handle before adding again in order to retry request.
curl_multi_remove_handle($this->multiCurl, $ch->curl);
@@ -689,6 +688,8 @@ class MultiCurl
);
}
} else {
$ch->execDone();
// Remove completed instance from active curls.
unset($this->activeCurls[$key]);
@@ -821,7 +822,7 @@ class MultiCurl
$curl->setOpts($this->options);
$curl->setHeaders($this->headers);
$curl->setRetry($this->maximumNumberOfRetries);
$curl->setRetry($this->retry);
foreach ($this->cookies as $key => $value) {
$curl->setCookie($key, $value);