Class: Synapse::Repository::OptimisticLockManager

Inherits:
LockManager
  • Object
show all
Defined in:
lib/synapse/repository/optimistic_lock_manager.rb

Overview

Lock manager that uses an optimistic locking strategy

This implementation uses the sequence number of an aggregate’s last committed event to detect concurrenct access.

Instance Method Summary collapse

Constructor Details

#initializeundefined



9
10
11
12
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 9

def initialize
  @aggregates = Hash.new
  @mutex = Mutex.new
end

Instance Method Details

#obtain_lock(aggregate_id) ⇒ undefined

Parameters:

  • aggregate_id (Object)

Returns:

  • (undefined)


22
23
24
25
26
27
28
29
30
31
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 22

def obtain_lock(aggregate_id)
  obtained = false
  until obtained
    lock = lock_for aggregate_id
    obtained = lock && lock.lock
    unless obtained
      remove_lock aggregate_id, lock
    end
  end
end

#release_lock(aggregate_id) ⇒ undefined

Parameters:

  • aggregate_id (Object)

Returns:

  • (undefined)


35
36
37
38
39
40
41
42
43
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 35

def release_lock(aggregate_id)
  lock = @aggregates[aggregate_id]
  if lock
    lock.unlock
    if lock.closed?
      remove_lock aggregate_id, lock
    end
  end
end

#validate_lock(aggregate) ⇒ Boolean

Parameters:

  • aggregate (AggregateRoot)

Returns:

  • (Boolean)


16
17
18
# File 'lib/synapse/repository/optimistic_lock_manager.rb', line 16

def validate_lock(aggregate)
  @aggregates.has_key?(aggregate.id) && @aggregates[aggregate.id].validate(aggregate)
end