Skip to content

Commit

Permalink
Merge pull request #117 from utopia-php/feat-s3-retry
Browse files Browse the repository at this point in the history
Feat: retry with delay in s3 adapter
  • Loading branch information
lohanidamodar authored Nov 6, 2024
2 parents fca5912 + eb932aa commit fdae7bf
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class S3 extends Device

protected const MAX_PAGE_SIZE = 1000;

protected static int $retryAttempts = 3;

protected static int $retryDelay = 500; // milliseconds

/**
* @var string
*/
Expand Down Expand Up @@ -242,6 +246,28 @@ public function setHttpVersion(?int $httpVersion): self
return $this;
}

/**
* Set retry attempts
*
* @param int $attempts
* @return void
*/
public static function setRetryAttempts(int $attempts)
{
self::$retryAttempts = $attempts;
}

/**
* Set retry delay in milliseconds
*
* @param int $delay
* @return void
*/
public static function setRetryDelay(int $delay): void
{
self::$retryDelay = $delay;
}

/**
* Upload.
*
Expand Down Expand Up @@ -915,11 +941,20 @@ protected function call(string $method, string $uri, string $data = '', array $p

$result = \curl_exec($curl);

$response->code = \curl_getinfo($curl, CURLINFO_HTTP_CODE);

$attempt = 0;
while ($attempt < self::$retryAttempts && $response->code === 503) {
usleep(self::$retryDelay * 1000);
$attempt++;
$result = \curl_exec($curl);
$response->code = \curl_getinfo($curl, CURLINFO_HTTP_CODE);
}

if (! $result) {
throw new Exception(\curl_error($curl));
}

$response->code = \curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($response->code >= 400) {
throw new Exception($response->body, $response->code);
}
Expand Down

0 comments on commit fdae7bf

Please sign in to comment.