-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
194 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace ChartMogul\Http; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use STS\Backoff\Backoff; | ||
|
||
class ExponentialStrategy extends \STS\Backoff\Strategies\ExponentialStrategy | ||
{ | ||
// min wait time | ||
protected $base = 1000; | ||
} | ||
|
||
class Retry | ||
{ | ||
|
||
private $retries; | ||
|
||
public function __construct($retries) | ||
{ | ||
$this->retries = $retries; | ||
} | ||
|
||
private function retryHTTPStatus($status) | ||
{ | ||
return $status == 429 || ($status >= 500 && $status < 600); | ||
} | ||
|
||
protected function shouldRetry($attempt, $maxAttempts, ResponseInterface $response = null, \Exception $ex = null) | ||
{ | ||
if ($attempt >= $maxAttempts && ! is_null($ex)) { | ||
throw $ex; | ||
} | ||
|
||
if (!is_null($response)) { | ||
return $attempt < $maxAttempts && $this->retryHTTPStatus($response->getStatusCode()); | ||
} | ||
|
||
// retry on all network related errors | ||
return $attempt < $maxAttempts && $ex instanceof \Http\Client\Exception\NetworkException; | ||
} | ||
|
||
public function retry($callback) | ||
{ | ||
if ($this->retries === 0) { | ||
return $callback(); | ||
} | ||
$backoff = new Backoff($this->retries, new ExponentialStrategy(), 60*1000, true); | ||
$backoff->setDecider($this); | ||
return $backoff->run($callback); | ||
} | ||
|
||
public function __invoke($attempt, $maxAttempts, $response, $exception) | ||
{ | ||
return $this->shouldRetry($attempt, $maxAttempts, $response, $exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters