gitlab-org--gitlab-foss/lib/gitlab/optimistic_locking.rb
Andrew Newdigate 56ae34e49b Adds metrics to measure database transactions
Currently we don't have good insight into the affect of Rails
transaction blocks on the application. If these blocks are held open for
extended periods, they can have detrimental effects on the application.

This change will allow us to track these transactions, with the aim
of reducing their duration.
2019-06-28 12:46:51 +02:00

22 lines
545 B
Ruby

# frozen_string_literal: true
module Gitlab
module OptimisticLocking
module_function
def retry_lock(subject, retries = 100, &block)
# TODO(Observability): We should be recording details of the number of retries and the duration of the total execution here
ActiveRecord::Base.transaction do
yield(subject)
end
rescue ActiveRecord::StaleObjectError
retries -= 1
raise unless retries >= 0
subject.reset
retry
end
alias_method :retry_optimistic_lock, :retry_lock
end
end