summaryrefslogtreecommitdiff
path: root/app/models/concerns/reactive_caching.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-10 15:08:54 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-10 15:08:54 +0000
commit11e5d1b9ca3efa7be34ddebb708a6aedb4e91639 (patch)
tree999fdffb9d3db2e5200994e289e50fa3a3a1684a /app/models/concerns/reactive_caching.rb
parent7351a484d79236b7e9d47c86f2fcc970b7ae10b0 (diff)
downloadgitlab-ce-11e5d1b9ca3efa7be34ddebb708a6aedb4e91639.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/concerns/reactive_caching.rb')
-rw-r--r--app/models/concerns/reactive_caching.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/app/models/concerns/reactive_caching.rb b/app/models/concerns/reactive_caching.rb
index 4b9896343c6..28d65e0bd45 100644
--- a/app/models/concerns/reactive_caching.rb
+++ b/app/models/concerns/reactive_caching.rb
@@ -6,23 +6,22 @@ module ReactiveCaching
extend ActiveSupport::Concern
InvalidateReactiveCache = Class.new(StandardError)
+ ExceededReactiveCacheLimit = Class.new(StandardError)
included do
- class_attribute :reactive_cache_lease_timeout
-
class_attribute :reactive_cache_key
- class_attribute :reactive_cache_lifetime
+ class_attribute :reactive_cache_lease_timeout
class_attribute :reactive_cache_refresh_interval
+ class_attribute :reactive_cache_lifetime
+ class_attribute :reactive_cache_hard_limit
class_attribute :reactive_cache_worker_finder
# defaults
self.reactive_cache_key = -> (record) { [model_name.singular, record.id] }
-
self.reactive_cache_lease_timeout = 2.minutes
-
self.reactive_cache_refresh_interval = 1.minute
self.reactive_cache_lifetime = 10.minutes
-
+ self.reactive_cache_hard_limit = 1.megabyte
self.reactive_cache_worker_finder = ->(id, *_args) do
find_by(primary_key => id)
end
@@ -71,6 +70,8 @@ module ReactiveCaching
if within_reactive_cache_lifetime?(*args)
enqueuing_update(*args) do
new_value = calculate_reactive_cache(*args)
+ check_exceeded_reactive_cache_limit!(new_value)
+
old_value = Rails.cache.read(key)
Rails.cache.write(key, new_value)
reactive_cache_updated(*args) if new_value != old_value
@@ -121,5 +122,13 @@ module ReactiveCaching
ReactiveCachingWorker.perform_in(self.class.reactive_cache_refresh_interval, self.class, id, *args)
end
+
+ def check_exceeded_reactive_cache_limit!(data)
+ return unless Feature.enabled?(:reactive_cache_limit)
+
+ data_deep_size = Gitlab::Utils::DeepSize.new(data, max_size: self.class.reactive_cache_hard_limit)
+
+ raise ExceededReactiveCacheLimit.new unless data_deep_size.valid?
+ end
end
end