2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
module Gitlab
|
2016-10-27 07:34:09 -04:00
|
|
|
module OptimisticLocking
|
2017-02-23 05:44:49 -05:00
|
|
|
module_function
|
2016-10-27 07:34:09 -04:00
|
|
|
|
|
|
|
def retry_lock(subject, retries = 100, &block)
|
2019-06-21 05:18:19 -04:00
|
|
|
# TODO(Observability): We should be recording details of the number of retries and the duration of the total execution here
|
2018-04-18 05:19:40 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
yield(subject)
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
2018-04-18 05:19:40 -04:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
retries -= 1
|
|
|
|
raise unless retries >= 0
|
|
|
|
|
2019-04-08 09:33:36 -04:00
|
|
|
subject.reset
|
2018-04-18 05:19:40 -04:00
|
|
|
retry
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
2017-02-23 05:44:49 -05:00
|
|
|
|
2017-02-23 08:56:27 -05:00
|
|
|
alias_method :retry_optimistic_lock, :retry_lock
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
|
|
|
end
|