Permalink
Browse files

Fixes #3. API call exponential backoff/retry

  • Loading branch information...
thousandsofthem committed Mar 4, 2012
1 parent d0beb8c commit 66c5d3b90631d167344741de366fef28e2bda6b7
Showing with 32 additions and 14 deletions.
  1. +29 −13 IronMQ.class.php
  2. +2 −0 README.md
  3. +1 −1 simpleTest.php
View
@@ -173,7 +173,8 @@ class IronMQ{
const GET = 'GET';
const DELETE = 'DELETE';
- public $debug_enabled = false;
+ public $debug_enabled = false;
+ public $max_retries = 5;
private $required_config_fields = array('token','project_id');
private $default_values = array(
@@ -381,19 +382,34 @@ private function apiCall($type, $url, $params = array()){
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_HTTPHEADER, $this->compiledHeaders());
- $_out = curl_exec($s);
- $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
- curl_close($s);
- switch ($status) {
- case self::HTTP_OK:
- case self::HTTP_CREATED:
- case self::HTTP_ACEPTED:
- $out = $_out;
- break;
- default:
- throw new Http_Exception("http error: {$status} | {$_out}", $status);
+
+ return $this->callWithRetries($s);
+ }
+
+
+ private function callWithRetries($s){
+ for ($retry = 0; $retry < $this->max_retries; $retry++){
+ $_out = curl_exec($s);
+ $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
+ switch ($status) {
+ case self::HTTP_OK:
+ case self::HTTP_CREATED:
+ case self::HTTP_ACEPTED:
+ curl_close($s);
+ return $_out;
+ case Http_Exception::BAD_REQUEST:
+ case Http_Exception::NOT_FOUND:
+ case Http_Exception::NOT_ALOWED:
+ case Http_Exception::CONFLICT:
+ case Http_Exception::PRECONDITION_FAILED:
+ throw new Http_Exception("http error: {$status} | {$_out}", $status);
+ default:
+ // wait for a random delay between 0 and (4^currentRetry * 100) milliseconds
+ $max_delay = pow(4, $retry)*100*1000;
+ usleep(rand(0, $max_delay));
+ }
}
- return $out;
+ throw new Http_Exception("http error: Max retries count is reached", 500);
}
View
@@ -1,5 +1,7 @@
PHP language binding for IronMQ
+
[IronMQ](http://www.iron.io/products/mq) is an elastic message queue for managing data and event flow within cloud applications and between systems.
+
[See How It Works](http://www.iron.io/products/mq/how)
# Getting Started
View
@@ -9,7 +9,7 @@
$res = $ironmq->postMessage("test_queue", array("body" => "Test Message"));
print_r($res);
-sleep(15);
+sleep(2);
print "Getting message..";
$message = $ironmq->getMessage("test_queue");
print_r($message);

2 comments on commit 66c5d3b

@treeder

This comment has been minimized.

Show comment Hide comment
@treeder

treeder Mar 4, 2012

Hey @thousandsofthem , I think we should only retry if 503 right now, otherwise it should error out.

Hey @thousandsofthem , I think we should only retry if 503 right now, otherwise it should error out.

@thousandsofthem

This comment has been minimized.

Show comment Hide comment
@thousandsofthem

thousandsofthem Mar 4, 2012

Contributor

Not 5xx, just 503? Right now 2xx and 4xx codes passes through, all other codes causes retry.

Contributor

thousandsofthem replied Mar 4, 2012

Not 5xx, just 503? Right now 2xx and 4xx codes passes through, all other codes causes retry.

Please sign in to comment.