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

#initializeOptimisticLockManager

Returns a new instance of OptimisticLockManager.



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

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

Instance Method Details

#obtain_lock(aggregate_id) ⇒ undefined

Parameters:

  • aggregate_id (Object)

Returns:

  • (undefined)


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

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

#release_lock(aggregate_id) ⇒ undefined

Parameters:

  • aggregate_id (Object)

Returns:

  • (undefined)


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

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)


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

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