From c8d5e3102b2d50f9b9974a7d8f49da4a54a7c76e Mon Sep 17 00:00:00 2001 From: romanetar Date: Wed, 19 Apr 2023 17:30:04 +0200 Subject: [PATCH 1/2] bypass for a period of time subsequent invocations on connection error to Swift Signed-off-by: romanetar --- app/Models/Foundation/Main/File.php | 14 ++++--- .../FileSystem/Swift/SwiftServiceProvider.php | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/app/Models/Foundation/Main/File.php b/app/Models/Foundation/Main/File.php index b660e67f6..ed7de9cc0 100644 --- a/app/Models/Foundation/Main/File.php +++ b/app/Models/Foundation/Main/File.php @@ -279,9 +279,10 @@ public function getCloudLink() { try { $relativeLink = ltrim($this->getRelativeLinkFor(), '/'); - return Storage::disk(Config::get('filesystems.assets_disk', 'assets') )->url($relativeLink); - } - catch (\Exception $ex){ + $disk = Storage::disk(Config::get('filesystems.assets_disk', 'assets')); + if ($disk == null) return null; + return $disk->url($relativeLink); + } catch (\Exception $ex) { Log::warning($ex); return null; } @@ -294,9 +295,10 @@ public function getCloudLink() public static function getCloudLinkForImages(string $imageRelativePath):?string { try { $imageRelativePath = ltrim($imageRelativePath, '/'); - return Storage::disk(Config::get('filesystems.static_images_disk', 'static_images'))->url($imageRelativePath); - } - catch (\Exception $ex){ + $disk = Storage::disk(Config::get('filesystems.static_images_disk', 'static_images')); + if ($disk == null) return null; + return $disk->url($imageRelativePath); + } catch (\Exception $ex) { Log::warning($ex); return null; } diff --git a/app/Services/FileSystem/Swift/SwiftServiceProvider.php b/app/Services/FileSystem/Swift/SwiftServiceProvider.php index 3f1afba49..e51f56c07 100644 --- a/app/Services/FileSystem/Swift/SwiftServiceProvider.php +++ b/app/Services/FileSystem/Swift/SwiftServiceProvider.php @@ -12,6 +12,8 @@ * limitations under the License. **/ +use Carbon\Carbon; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use League\Flysystem\Filesystem; @@ -23,6 +25,35 @@ */ final class SwiftServiceProvider extends ServiceProvider { + const MAX_SKIPS = 4; + + private function shouldBypass(): bool + { + $skip_until = Cache::tags(SwiftServiceProvider::class)->get('skip_until', Carbon::now()); + return Carbon::now() < $skip_until; + } + + private function recalculateSkipDelay() + { + $cache = Cache::tags(SwiftServiceProvider::class); + $skip_count = intval($cache->get('skip_count', 0)); + $skip_delay = intval($cache->get('skip_delay', 1)); + $skip_count++; + $skip_delay = $skip_delay * pow(2, $skip_count - 1); + $skip_until = Carbon::now()->add($skip_delay, 'minutes'); + $cache->put('skip_count', $skip_count); + $cache->put('skip_delay', $skip_delay); + $cache->put('skip_until', $skip_until); + if ($skip_count > self::MAX_SKIPS) { + $this->resetSkips(); + } + } + + private function resetSkips() + { + Cache::tags(SwiftServiceProvider::class)->flush(); + } + /** * Register bindings in the container. * @@ -43,6 +74,8 @@ public function boot() Storage::extend('swift', function ($app, $config) { try { + if ($this->shouldBypass()) return null; + $configOptions = [ 'authUrl' => $config["auth_url"], 'region' => $config["region"], @@ -81,12 +114,15 @@ public function boot() $container = $openstackClient->objectStoreV1()->getContainer($config["container"]); + $this->resetSkips(); + return new Filesystem(new SwiftAdapter($container)); } catch(\Exception $ex){ Log::error($ex); + $this->recalculateSkipDelay(); return null; } }); } -} +} \ No newline at end of file From 81c4e4d2063dab596170eec25db972908fe34f12 Mon Sep 17 00:00:00 2001 From: romanetar Date: Wed, 19 Apr 2023 18:40:58 +0200 Subject: [PATCH 2/2] bypass for a period of time subsequent invocations on connection error to Swift Signed-off-by: romanetar --- app/Services/FileSystem/Swift/SwiftServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/Services/FileSystem/Swift/SwiftServiceProvider.php b/app/Services/FileSystem/Swift/SwiftServiceProvider.php index e51f56c07..873b17785 100644 --- a/app/Services/FileSystem/Swift/SwiftServiceProvider.php +++ b/app/Services/FileSystem/Swift/SwiftServiceProvider.php @@ -74,6 +74,8 @@ public function boot() Storage::extend('swift', function ($app, $config) { try { + if (empty($config["auth_url"]) || empty($config["region"]) || empty($config["container"])) return null; + if ($this->shouldBypass()) return null; $configOptions = [