summaryrefslogtreecommitdiff
path: root/lib/gitlab/optimistic_locking.rb
blob: 0c0f46d3b7758518564f1490b6c651f7c3afea38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

module Gitlab
  module OptimisticLocking
    module_function

    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)
      end
    rescue ActiveRecord::StaleObjectError
      retries -= 1
      raise unless retries >= 0

      subject.reset
      retry
    end

    alias_method :retry_optimistic_lock, :retry_lock
  end
end