gitlab-org--gitlab-foss/lib/gitlab/optimistic_locking.rb

23 lines
545 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-10-20 07:33:44 +00:00
module Gitlab
2016-10-27 11:34:09 +00:00
module OptimisticLocking
module_function
2016-10-27 11:34:09 +00:00
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)
2016-10-20 07:33:44 +00:00
end
rescue ActiveRecord::StaleObjectError
retries -= 1
raise unless retries >= 0
subject.reset
retry
2016-10-20 07:33:44 +00:00
end
alias_method :retry_optimistic_lock, :retry_lock
2016-10-20 07:33:44 +00:00
end
end