Pull 135: Backblaze B2 storage driver #138
25
app/Exceptions/BackblazeRetryException.php
Normal file
25
app/Exceptions/BackblazeRetryException.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user