From 60a62b59c9184bbd079747b70952af119e4d40b1 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 7 May 2024 01:00:32 -0600 Subject: [PATCH] Update ConfigCacheService, fix database race condition and fallback to file config and enable by default --- .env.docker | 2 +- .env.example | 2 +- app/Services/ConfigCacheService.php | 258 ++++++++++++++-------------- 3 files changed, 133 insertions(+), 129 deletions(-) diff --git a/.env.docker b/.env.docker index e1143f5e7..92c01756b 100644 --- a/.env.docker +++ b/.env.docker @@ -67,7 +67,7 @@ ADMIN_DOMAIN="${APP_DOMAIN}" # @default "false" # @see https://docs.pixelfed.org/technical-documentation/config/#config_cache # @dottie/validate required,boolean -ENABLE_CONFIG_CACHE="false" +ENABLE_CONFIG_CACHE="true" # Enable/disable new local account registrations. # diff --git a/.env.example b/.env.example index 84c7d6433..79ce65337 100644 --- a/.env.example +++ b/.env.example @@ -8,7 +8,7 @@ OPEN_REGISTRATION="false" ENFORCE_EMAIL_VERIFICATION="false" PF_MAX_USERS="1000" OAUTH_ENABLED="true" -ENABLE_CONFIG_CACHE=false +ENABLE_CONFIG_CACHE=true # Media Configuration PF_OPTIMIZE_IMAGES="true" diff --git a/app/Services/ConfigCacheService.php b/app/Services/ConfigCacheService.php index 626982781..b18c02e36 100644 --- a/app/Services/ConfigCacheService.php +++ b/app/Services/ConfigCacheService.php @@ -4,6 +4,7 @@ namespace App\Services; use App\Models\ConfigCache as ConfigCacheModel; use Cache; +use Illuminate\Database\QueryException; class ConfigCacheService { @@ -25,156 +26,159 @@ class ConfigCacheService return config($key); } - return Cache::remember($cacheKey, $ttl, function () use ($key) { + try { + return Cache::remember($cacheKey, $ttl, function () use ($key) { + $allowed = [ + 'app.name', + 'app.short_description', + 'app.description', + 'app.rules', - $allowed = [ - 'app.name', - 'app.short_description', - 'app.description', - 'app.rules', + 'pixelfed.max_photo_size', + 'pixelfed.max_album_length', + 'pixelfed.image_quality', + 'pixelfed.media_types', - 'pixelfed.max_photo_size', - 'pixelfed.max_album_length', - 'pixelfed.image_quality', - 'pixelfed.media_types', + 'pixelfed.open_registration', + 'federation.activitypub.enabled', + 'instance.stories.enabled', + 'pixelfed.oauth_enabled', + 'pixelfed.import.instagram.enabled', + 'pixelfed.bouncer.enabled', - 'pixelfed.open_registration', - 'federation.activitypub.enabled', - 'instance.stories.enabled', - 'pixelfed.oauth_enabled', - 'pixelfed.import.instagram.enabled', - 'pixelfed.bouncer.enabled', + 'pixelfed.enforce_email_verification', + 'pixelfed.max_account_size', + 'pixelfed.enforce_account_limit', - 'pixelfed.enforce_email_verification', - 'pixelfed.max_account_size', - 'pixelfed.enforce_account_limit', + 'uikit.custom.css', + 'uikit.custom.js', + 'uikit.show_custom.css', + 'uikit.show_custom.js', + 'about.title', - 'uikit.custom.css', - 'uikit.custom.js', - 'uikit.show_custom.css', - 'uikit.show_custom.js', - 'about.title', + 'pixelfed.cloud_storage', - 'pixelfed.cloud_storage', + 'account.autofollow', + 'account.autofollow_usernames', + 'config.discover.features', - 'account.autofollow', - 'account.autofollow_usernames', - 'config.discover.features', + 'instance.has_legal_notice', + 'instance.avatar.local_to_cloud', - 'instance.has_legal_notice', - 'instance.avatar.local_to_cloud', + 'pixelfed.directory', + 'app.banner_image', + 'pixelfed.directory.submission-key', + 'pixelfed.directory.submission-ts', + 'pixelfed.directory.has_submitted', + 'pixelfed.directory.latest_response', + 'pixelfed.directory.is_synced', + 'pixelfed.directory.testimonials', - 'pixelfed.directory', - 'app.banner_image', - 'pixelfed.directory.submission-key', - 'pixelfed.directory.submission-ts', - 'pixelfed.directory.has_submitted', - 'pixelfed.directory.latest_response', - 'pixelfed.directory.is_synced', - 'pixelfed.directory.testimonials', + 'instance.landing.show_directory', + 'instance.landing.show_explore', + 'instance.admin.pid', + 'instance.banner.blurhash', - 'instance.landing.show_directory', - 'instance.landing.show_explore', - 'instance.admin.pid', - 'instance.banner.blurhash', + 'autospam.nlp.enabled', - 'autospam.nlp.enabled', + 'instance.curated_registration.enabled', - 'instance.curated_registration.enabled', + 'federation.migration', - 'federation.migration', + 'pixelfed.max_caption_length', + 'pixelfed.max_bio_length', + 'pixelfed.max_name_length', + 'pixelfed.min_password_length', + 'pixelfed.max_avatar_size', + 'pixelfed.max_altext_length', + 'pixelfed.allow_app_registration', + 'pixelfed.app_registration_rate_limit_attempts', + 'pixelfed.app_registration_rate_limit_decay', + 'pixelfed.app_registration_confirm_rate_limit_attempts', + 'pixelfed.app_registration_confirm_rate_limit_decay', + 'instance.embed.profile', + 'instance.embed.post', - 'pixelfed.max_caption_length', - 'pixelfed.max_bio_length', - 'pixelfed.max_name_length', - 'pixelfed.min_password_length', - 'pixelfed.max_avatar_size', - 'pixelfed.max_altext_length', - 'pixelfed.allow_app_registration', - 'pixelfed.app_registration_rate_limit_attempts', - 'pixelfed.app_registration_rate_limit_decay', - 'pixelfed.app_registration_confirm_rate_limit_attempts', - 'pixelfed.app_registration_confirm_rate_limit_decay', - 'instance.embed.profile', - 'instance.embed.post', + 'captcha.enabled', + 'captcha.secret', + 'captcha.sitekey', + 'captcha.active.login', + 'captcha.active.register', + 'captcha.triggers.login.enabled', + 'captcha.triggers.login.attempts', + 'federation.custom_emoji.enabled', - 'captcha.enabled', - 'captcha.secret', - 'captcha.sitekey', - 'captcha.active.login', - 'captcha.active.register', - 'captcha.triggers.login.enabled', - 'captcha.triggers.login.attempts', - 'federation.custom_emoji.enabled', + 'pixelfed.optimize_image', + 'pixelfed.optimize_video', + 'pixelfed.max_collection_length', + 'media.delete_local_after_cloud', + 'instance.user_filters.max_user_blocks', + 'instance.user_filters.max_user_mutes', + 'instance.user_filters.max_domain_blocks', - 'pixelfed.optimize_image', - 'pixelfed.optimize_video', - 'pixelfed.max_collection_length', - 'media.delete_local_after_cloud', - 'instance.user_filters.max_user_blocks', - 'instance.user_filters.max_user_mutes', - 'instance.user_filters.max_domain_blocks', + 'filesystems.disks.s3.key', + 'filesystems.disks.s3.secret', + 'filesystems.disks.s3.region', + 'filesystems.disks.s3.bucket', + 'filesystems.disks.s3.visibility', + 'filesystems.disks.s3.url', + 'filesystems.disks.s3.endpoint', + 'filesystems.disks.s3.use_path_style_endpoint', - 'filesystems.disks.s3.key', - 'filesystems.disks.s3.secret', - 'filesystems.disks.s3.region', - 'filesystems.disks.s3.bucket', - 'filesystems.disks.s3.visibility', - 'filesystems.disks.s3.url', - 'filesystems.disks.s3.endpoint', - 'filesystems.disks.s3.use_path_style_endpoint', + 'filesystems.disks.spaces.key', + 'filesystems.disks.spaces.secret', + 'filesystems.disks.spaces.region', + 'filesystems.disks.spaces.bucket', + 'filesystems.disks.spaces.visibility', + 'filesystems.disks.spaces.url', + 'filesystems.disks.spaces.endpoint', + 'filesystems.disks.spaces.use_path_style_endpoint', + // 'system.user_mode' + ]; - 'filesystems.disks.spaces.key', - 'filesystems.disks.spaces.secret', - 'filesystems.disks.spaces.region', - 'filesystems.disks.spaces.bucket', - 'filesystems.disks.spaces.visibility', - 'filesystems.disks.spaces.url', - 'filesystems.disks.spaces.endpoint', - 'filesystems.disks.spaces.use_path_style_endpoint', - // 'system.user_mode' - ]; - - if (! config('instance.enable_cc')) { - return config($key); - } - - if (! in_array($key, $allowed)) { - return config($key); - } - - $protect = false; - $protected = null; - if(in_array($key, self::PROTECTED_KEYS)) { - $protect = true; - } - - $v = config($key); - $c = ConfigCacheModel::where('k', $key)->first(); - - if ($c) { - if($protect) { - return decrypt($c->v) ?? config($key); - } else { - return $c->v ?? config($key); + if (! config('instance.enable_cc')) { + return config($key); } - } - if (! $v) { - return; - } + if (! in_array($key, $allowed)) { + return config($key); + } - if($protect && $v) { - $protected = encrypt($v); - } + $protect = false; + $protected = null; + if(in_array($key, self::PROTECTED_KEYS)) { + $protect = true; + } - $cc = new ConfigCacheModel; - $cc->k = $key; - $cc->v = $protect ? $protected : $v; - $cc->save(); + $v = config($key); + $c = ConfigCacheModel::where('k', $key)->first(); - return $v; - }); + if ($c) { + if($protect) { + return decrypt($c->v) ?? config($key); + } else { + return $c->v ?? config($key); + } + } + + if (! $v) { + return; + } + + if($protect && $v) { + $protected = encrypt($v); + } + + $cc = new ConfigCacheModel; + $cc->k = $key; + $cc->v = $protect ? $protected : $v; + $cc->save(); + + return $v; + }); + } catch (Exception | QueryException $e) { + return config($key); + } } public static function put($key, $val)