Backblaze #135 - implemented a retry and backoff period for 500/503 errors

This commit is contained in:
Andy Heathershaw 2019-09-11 14:59:25 +01:00
parent fb6754b8e9
commit 69422ffaa4
2 changed files with 56 additions and 1 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace App\Exceptions;
use Throwable;
class BackblazeRetryException extends \Exception
{
private $innerException;
/**
* @return mixed
*/
public function getInnerException()
{
return $this->innerException;
}
public function __construct($httpCode, \Exception $innerException)
{
parent::__construct('Backblaze requested to retry the request');
$this->innerException = $innerException;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Services; namespace App\Services;
use App\Exceptions\BackblazeRetryException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class BackblazeB2Service class BackblazeB2Service
@ -238,6 +239,27 @@ class BackblazeB2Service
} }
private function sendRequest($url, $method = 'GET', $postData = null, array $postOptions = []) private function sendRequest($url, $method = 'GET', $postData = null, array $postOptions = [])
{
$exponentialBackoff = 1;
$numberOfRetries = 5; // this effectively gives us 31 seconds of retries (1+2+4+8+16)
$numberOfTimesTried = 0;
while ($numberOfTimesTried < $numberOfRetries)
{
try
{
return $this->sendRequestReal($url, $method, $postData, $postOptions);
}
catch (BackblazeRetryException $ex)
{
// Keep backing off
$exponentialBackoff *= $exponentialBackoff;
$numberOfTimesTried++;
}
}
}
private function sendRequestReal($url, $method = 'GET', $postData = null, array $postOptions = [])
{ {
$postOptions = array_merge( $postOptions = array_merge(
[ [
@ -298,7 +320,15 @@ class BackblazeB2Service
Log::info(sprintf('Received HTTP code %d', $httpCode)); Log::info(sprintf('Received HTTP code %d', $httpCode));
Log::debug($result); Log::debug($result);
if ($httpCode != 200 && $httpCode != 304) // According to the Backblaze B2 Protocol, if we get a 500/503, we should retry the request
if ($httpCode == 500 || $httpCode == 503)
{
throw new BackblazeRetryException(
$httpCode,
new \Exception(sprintf('Exception from Backblaze B2: %s', $result))
);
}
else if ($httpCode != 200 && $httpCode != 304)
{ {
throw new \Exception(sprintf('Exception from Backblaze B2: %s', $result)); throw new \Exception(sprintf('Exception from Backblaze B2: %s', $result));
} }