Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: s3 retry #118

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading