debian-mirror-gitlab/lib/gitlab/optimistic_locking.rb

23 lines
545 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module OptimisticLocking
module_function
def retry_lock(subject, retries = 100, &block)
2019-09-30 21:07:59 +05:30
# TODO(Observability): We should be recording details of the number of retries and the duration of the total execution here
2018-10-15 14:42:47 +05:30
ActiveRecord::Base.transaction do
yield(subject)
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
rescue ActiveRecord::StaleObjectError
retries -= 1
raise unless retries >= 0
2019-07-31 22:56:46 +05:30
subject.reset
2018-10-15 14:42:47 +05:30
retry
2017-08-17 22:00:37 +05:30
end
alias_method :retry_optimistic_lock, :retry_lock
end
end