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
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def retry_lock(subject, retries = nil, &block)
|
|
|
|
retries ||= 100
|
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
|