optimistic_locking.rb 567 Bytes
Newer Older
1 2
# frozen_string_literal: true

3
module Gitlab
4
  module OptimisticLocking
5
    module_function
6

7
    def retry_lock(subject, retries = nil, &block)
8
      retries ||= 100
9
      # TODO(Observability): We should be recording details of the number of retries and the duration of the total execution here
10 11
      ActiveRecord::Base.transaction do
        yield(subject)
12
      end
13 14 15 16
    rescue ActiveRecord::StaleObjectError
      retries -= 1
      raise unless retries >= 0

17
      subject.reset
18
      retry
19
    end
20

21
    alias_method :retry_optimistic_lock, :retry_lock
22 23
  end
end