Class: Royal::Locking::Optimistic

Inherits:
Object
  • Object
show all
Defined in:
lib/royal/locking/optimistic.rb

Instance Method Summary collapse

Instance Method Details

#call(_owner, &block) ⇒ Object

Parameters:

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/royal/locking/optimistic.rb', line 7

def call(_owner, &block)
  result = nil

  up_to_max_retries do
    success, result = try_create_record(&block)
    return result if success
  end

  # NOTE: Failed to insert record after maximum number of attempts.
  # This could be caused by too much write contention for the same owner.
  # One possible solution is to acquire a row-level lock on the owner record and retry.
  # Other solutions like advisory locks or sequential processing queues may work better in some situations.
  raise Royal::SequenceError, "Failed to update points: #{result.message}"
end