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

24 lines
567 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-10-20 03:33:44 -04:00
module Gitlab
2016-10-27 07:34:09 -04:00
module OptimisticLocking
module_function
2016-10-27 07:34:09 -04:00
2019-09-02 04:44:52 -04:00
def retry_lock(subject, retries = nil, &block)
retries ||= 100
# 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 03:33:44 -04:00
end
rescue ActiveRecord::StaleObjectError
retries -= 1
raise unless retries >= 0
subject.reset
retry
2016-10-20 03:33:44 -04:00
end
alias_method :retry_optimistic_lock, :retry_lock
2016-10-20 03:33:44 -04:00
end
end