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 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
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 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