Skip to content

Commit 3c3741f

Browse files
committed
Implement Curl::setRetry()
1 parent 20d718e commit 3c3741f

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Curl::setOpts($options)
238238
Curl::setPort($port)
239239
Curl::setReferer($referer)
240240
Curl::setReferrer($referrer)
241+
Curl::setRetry($maximum_number_of_retries = 0)
241242
Curl::setTimeout($seconds)
242243
Curl::setUrl($url, $mixed_data = '')
243244
Curl::setUserAgent($user_agent)

src/Curl/Curl.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Curl
4141
public $completeFunction = null;
4242
public $fileHandle = null;
4343

44+
public $attempts = 0;
45+
public $retries = 0;
46+
private $maximumNumberOfRetries = 0;
47+
private $remainingRetries = 0;
48+
4449
private $cookies = array();
4550
private $headers = array();
4651
private $options = array();
@@ -335,6 +340,8 @@ public function error($callback)
335340
*/
336341
public function exec($ch = null)
337342
{
343+
$this->attempts += 1;
344+
338345
if ($ch === null) {
339346
$this->responseCookies = array();
340347
$this->call($this->beforeSendFunction);
@@ -382,6 +389,12 @@ public function exec($ch = null)
382389
}
383390
$this->errorMessage = $this->curlError ? $this->curlErrorMessage : $this->httpErrorMessage;
384391

392+
if ($this->error && $this->remainingRetries >= 1) {
393+
$this->remainingRetries -= 1;
394+
$this->retries += 1;
395+
return $this->exec($ch);
396+
}
397+
385398
if ($this->error) {
386399
$this->call($this->errorFunction);
387400
} else {
@@ -1010,6 +1023,20 @@ public function setReferrer($referrer)
10101023
$this->setOpt(CURLOPT_REFERER, $referrer);
10111024
}
10121025

1026+
/**
1027+
* Set Retry
1028+
*
1029+
* Number of retries to attempt. Maximum number of attempts is $maximum_number_of_retries + 1.
1030+
*
1031+
* @access public
1032+
* @param $maximum_number_of_retries
1033+
*/
1034+
public function setRetry($maximum_number_of_retries = 0)
1035+
{
1036+
$this->maximumNumberOfRetries = $maximum_number_of_retries;
1037+
$this->remainingRetries = $this->maximumNumberOfRetries;
1038+
}
1039+
10131040
/**
10141041
* Set Timeout
10151042
*

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,4 +3247,71 @@ public function testGetInfo()
32473247
$this->assertArrayHasKey($key, $info);
32483248
}
32493249
}
3250+
3251+
public function testRetry()
3252+
{
3253+
$tests = array(
3254+
array(
3255+
'maximum_number_of_retries' => null,
3256+
'failures' => 0,
3257+
'expect_success' => true,
3258+
'expect_attempts' => 1,
3259+
'expect_retries' => 0,
3260+
),
3261+
array(
3262+
'maximum_number_of_retries' => 0,
3263+
'failures' => 0,
3264+
'expect_success' => true,
3265+
'expect_attempts' => 1,
3266+
'expect_retries' => 0,
3267+
),
3268+
array(
3269+
'maximum_number_of_retries' => 0,
3270+
'failures' => 1,
3271+
'expect_success' => false,
3272+
'expect_attempts' => 1,
3273+
'expect_retries' => 0,
3274+
),
3275+
array(
3276+
'maximum_number_of_retries' => 1,
3277+
'failures' => 1,
3278+
'expect_success' => true,
3279+
'expect_attempts' => 2,
3280+
'expect_retries' => 1,
3281+
),
3282+
array(
3283+
'maximum_number_of_retries' => 1,
3284+
'failures' => 2,
3285+
'expect_success' => false,
3286+
'expect_attempts' => 2,
3287+
'expect_retries' => 1,
3288+
),
3289+
array(
3290+
'maximum_number_of_retries' => 2,
3291+
'failures' => 2,
3292+
'expect_success' => true,
3293+
'expect_attempts' => 3,
3294+
'expect_retries' => 2,
3295+
),
3296+
);
3297+
foreach ($tests as $test) {
3298+
$maximum_number_of_retries = $test['maximum_number_of_retries'];
3299+
$failures = $test['failures'];
3300+
$expect_success = $test['expect_success'];
3301+
$expect_attempts = $test['expect_attempts'];
3302+
$expect_retries = $test['expect_retries'];
3303+
3304+
$test = new Test();
3305+
$test->curl->setOpt(CURLOPT_COOKIEJAR, '/dev/null');
3306+
3307+
if (!($maximum_number_of_retries === null)) {
3308+
$test->curl->setRetry($maximum_number_of_retries);
3309+
}
3310+
3311+
$test->server('retry', 'GET', array('failures' => $failures));
3312+
$this->assertEquals($expect_success, !$test->curl->error);
3313+
$this->assertEquals($expect_attempts, $test->curl->attempts);
3314+
$this->assertEquals($expect_retries, $test->curl->retries);
3315+
}
3316+
}
32503317
}

tests/PHPCurlClass/server.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,29 @@
322322
}
323323
}
324324

325+
exit;
326+
} elseif ($test === 'retry') {
327+
session_start();
328+
329+
if (isset($_SESSION['failures_remaining'])) {
330+
$failures_remaining = $_SESSION['failures_remaining'];
331+
} else {
332+
$failures_remaining = ((int)$_GET['failures']);
333+
$_SESSION['failures_remaining'] = $failures_remaining;
334+
}
335+
336+
if ($failures_remaining >= 1) {
337+
$_SESSION['failures_remaining'] -= 1;
338+
339+
header('HTTP/1.1 503 Service Unavailable');
340+
echo 'Service Unavailable';
341+
echo ' (remaining failures: ' . $_SESSION['failures_remaining'] . ')';
342+
exit;
343+
}
344+
345+
header('HTTP/1.1 202 Accepted');
346+
echo '202 Accepted';
347+
echo ' (remaining failures: ' . $_SESSION['failures_remaining'] . ')';
325348
exit;
326349
}
327350

0 commit comments

Comments
 (0)