From 7eb441939d3d94ff722a58d7607acf730f441878 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 6 Nov 2024 08:53:42 +0000 Subject: [PATCH 1/4] retry with delay in s3 adapter --- src/Storage/Device/S3.php | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index c319bc57..7d4deafb 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -102,6 +102,10 @@ class S3 extends Device protected const MAX_PAGE_SIZE = 1000; + protected static int $retryAttempts = 5; + + protected static int $retryDelay = 2; // seconds + /** * @var string */ @@ -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 seconds + * + * @param int $delay + * @return void + */ + public static function setRetryDelay(int $delay): void + { + self::$retryDelay = $delay; + } + /** * Upload. * @@ -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) { + sleep(self::$retryDelay); + $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); } From fce7a27c2dd472bd7466ff4a2822505e2b1a9f6e Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 6 Nov 2024 09:13:25 +0000 Subject: [PATCH 2/4] use ms --- src/Storage/Device/S3.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 7d4deafb..9805d685 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -104,7 +104,7 @@ class S3 extends Device protected static int $retryAttempts = 5; - protected static int $retryDelay = 2; // seconds + protected static int $retryDelay = 100; // milliseconds /** * @var string @@ -258,7 +258,7 @@ public static function setRetryAttempts(int $attempts) } /** - * Set retry delay in seconds + * Set retry delay in milliseconds * * @param int $delay * @return void @@ -945,7 +945,7 @@ protected function call(string $method, string $uri, string $data = '', array $p $attempt = 0; while ($attempt < self::$retryAttempts && $response->code === 503) { - sleep(self::$retryDelay); + usleep(self::$retryDelay); $attempt++; $result = \curl_exec($curl); $response->code = \curl_getinfo($curl, CURLINFO_HTTP_CODE); From baad5074d8bf3d83abdf4b1465fec3d8d38d091a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 6 Nov 2024 09:19:24 +0000 Subject: [PATCH 3/4] tweak defaults --- src/Storage/Device/S3.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 9805d685..77dd26b9 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -102,9 +102,9 @@ class S3 extends Device protected const MAX_PAGE_SIZE = 1000; - protected static int $retryAttempts = 5; + protected static int $retryAttempts = 3; - protected static int $retryDelay = 100; // milliseconds + protected static int $retryDelay = 500; // milliseconds /** * @var string From eb932aabb8060a7dbcb97cc4b04399c7e5ee6f88 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 6 Nov 2024 09:31:47 +0000 Subject: [PATCH 4/4] fix delay --- src/Storage/Device/S3.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 77dd26b9..7a9bfbb1 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -945,7 +945,7 @@ protected function call(string $method, string $uri, string $data = '', array $p $attempt = 0; while ($attempt < self::$retryAttempts && $response->code === 503) { - usleep(self::$retryDelay); + usleep(self::$retryDelay * 1000); $attempt++; $result = \curl_exec($curl); $response->code = \curl_getinfo($curl, CURLINFO_HTTP_CODE);