diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
commit | 05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 (patch) | |
tree | 11d0f2a6ec31c7793c184106cedc2ded3d9a2cc5 /app/models/concerns/counter_attribute.rb | |
parent | ec73467c23693d0db63a797d10194da9e72a74af (diff) |
Add latest changes from gitlab-org/gitlab@15-8-stable-eev15.8.0-rc42
Diffstat (limited to 'app/models/concerns/counter_attribute.rb')
-rw-r--r-- | app/models/concerns/counter_attribute.rb | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/app/models/concerns/counter_attribute.rb b/app/models/concerns/counter_attribute.rb index f1efbba67e1..784afd1f231 100644 --- a/app/models/concerns/counter_attribute.rb +++ b/app/models/concerns/counter_attribute.rb @@ -88,12 +88,20 @@ module CounterAttribute end def increment_counter(attribute, increment) - return if increment == 0 + return if increment.amount == 0 run_after_commit_or_now do new_value = counter(attribute).increment(increment) - log_increment_counter(attribute, increment, new_value) + log_increment_counter(attribute, increment.amount, new_value) + end + end + + def bulk_increment_counter(attribute, increments) + run_after_commit_or_now do + new_value = counter(attribute).bulk_increment(increments) + + log_increment_counter(attribute, increments.sum(&:amount), new_value) end end @@ -103,14 +111,22 @@ module CounterAttribute end end - def reset_counter!(attribute) + def initiate_refresh!(attribute) + raise ArgumentError, %(attribute "#{attribute}" cannot be refreshed) unless counter_attribute_enabled?(attribute) + detect_race_on_record(log_fields: { caller: __method__, attributes: attribute }) do - counter(attribute).reset! + counter(attribute).initiate_refresh! end log_clear_counter(attribute) end + def finalize_refresh(attribute) + raise ArgumentError, %(attribute "#{attribute}" cannot be refreshed) unless counter_attribute_enabled?(attribute) + + counter(attribute).finalize_refresh + end + def execute_after_commit_callbacks self.class.after_commit_callbacks.each do |callback| callback.call(self.reset) @@ -122,11 +138,17 @@ module CounterAttribute def build_counter_for(attribute) raise ArgumentError, %(attribute "#{attribute}" does not exist) unless has_attribute?(attribute) - if counter_attribute_enabled?(attribute) - Gitlab::Counters::BufferedCounter.new(self, attribute) - else - Gitlab::Counters::LegacyCounter.new(self, attribute) - end + return legacy_counter(attribute) unless counter_attribute_enabled?(attribute) + + buffered_counter(attribute) + end + + def legacy_counter(attribute) + Gitlab::Counters::LegacyCounter.new(self, attribute) + end + + def buffered_counter(attribute) + Gitlab::Counters::BufferedCounter.new(self, attribute) end def database_lock_key |