diff options
Diffstat (limited to 'app/models/ci/instance_variable.rb')
-rw-r--r-- | app/models/ci/instance_variable.rb | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/app/models/ci/instance_variable.rb b/app/models/ci/instance_variable.rb index c674f76d229..8245729a884 100644 --- a/app/models/ci/instance_variable.rb +++ b/app/models/ci/instance_variable.rb @@ -3,8 +3,13 @@ module Ci class InstanceVariable < ApplicationRecord extend Gitlab::Ci::Model + extend Gitlab::ProcessMemoryCache::Helper include Ci::NewHasVariable include Ci::Maskable + include Limitable + + self.limit_name = 'ci_instance_level_variables' + self.limit_scope = Limitable::GLOBAL_SCOPE alias_attribute :secret_value, :value @@ -12,8 +17,14 @@ module Ci message: "(%{value}) has already been taken" } + validates :encrypted_value, length: { + maximum: 1024, + too_long: 'The encrypted value of the provided variable exceeds %{count} bytes. Variables over 700 characters risk exceeding the limit.' + } + scope :unprotected, -> { where(protected: false) } - after_commit { self.class.touch_redis_cache_timestamp } + + after_commit { self.class.invalidate_memory_cache(:ci_instance_variable_data) } class << self def all_cached @@ -24,10 +35,6 @@ module Ci cached_data[:unprotected] end - def touch_redis_cache_timestamp(time = Time.current.to_f) - shared_backend.write(:ci_instance_variable_changed_at, time) - end - private def cached_data @@ -37,39 +44,13 @@ module Ci { all: all_records, unprotected: all_records.reject(&:protected?) } end end + end - def fetch_memory_cache(key, &payload) - cache = process_backend.read(key) - - if cache && !stale_cache?(cache) - cache[:data] - else - store_cache(key, &payload) - end - end - - def stale_cache?(cache_info) - shared_timestamp = shared_backend.read(:ci_instance_variable_changed_at) - return true unless shared_timestamp - - shared_timestamp.to_f > cache_info[:cached_at].to_f - end - - def store_cache(key) - data = yield - time = Time.current.to_f - - process_backend.write(key, data: data, cached_at: time) - touch_redis_cache_timestamp(time) - data - end - - def shared_backend - Rails.cache - end + private - def process_backend - Gitlab::ProcessMemoryCache.cache_backend + def validate_plan_limit_not_exceeded + if Gitlab::Ci::Features.instance_level_variables_limit_enabled? + super end end end |