Module: Xolo::Server::ObjectLocks

Included in:
Xolo::Server
Defined in:
lib/xolo/server/object_locks.rb

Overview

Code for locking titles and versions as they are being modified

Constant Summary collapse

OBJECT_LOCK_LIMIT =

How many seconds are update locks valid for?

60 * 60

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(extender) ⇒ Object

when this module is extended



25
26
27
# File 'lib/xolo/server/object_locks.rb', line 25

def self.extended(extender)
  Xolo.verbose_extend extender, self
end

.included(includer) ⇒ Object

when this module is included



20
21
22
# File 'lib/xolo/server/object_locks.rb', line 20

def self.included(includer)
  Xolo.verbose_include includer, self
end

Instance Method Details

#object_locksConcurrent::Hash

The locks for titles and versions - when they are being created or updated they appear in this data structure with an expiration timestamp. During that time, no other updates can be made to the same title or version.

Keys are the title names, values are hashes with these sub-keys:

  • expires: the time the title lock expires, time locked plus OBJECT_LOCK_LIMIT

  • versions: Hash of versions_that_are_locked => expiration_time

Returns:

  • (Concurrent::Hash)

    The locks



42
43
44
# File 'lib/xolo/server/object_locks.rb', line 42

def object_locks
  @object_locks ||= Concurrent::Hash.new
end

#remove_expired_object_locksObject

Remove any expired locks from the object_locks



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/xolo/server/object_locks.rb', line 48

def remove_expired_object_locks
  now = Time.now

  # first delete any expired version locks
  object_locks.each_value do |locks|
    locks[:versions] ||= {}

    locks[:versions].delete_if do |vers, exp|
      if exp < now
        log_debug "Removing expired lock on version #{vers} of title #{title}"
        true
      else
        false
      end
    end
  end

  # now delete any expired title locks that have no versions locked
  object_locks.delete_if do |_title, locks|
    # keep it if there are versions locked
    if !locks[:versions].empty?
      false

    # if there's a title lock expiration time, check it
    elsif locks[:expires]
      if locks[:expires] < now
        log_debug "Removing expired lock on title #{title}"
        true
      else
        false
      end

    # no title-lock expiration time
    else
      true
    end
  end
end

#rw_lock(title, version = nil) ⇒ Object

Testing Concurrent::ReentrantReadWriteLock for titles and versions to be acquired and released in the route blocks



90
91
92
93
94
95
96
# File 'lib/xolo/server/object_locks.rb', line 90

def rw_lock(title, version = nil)
  @rw_locks ||= Concurrent::Hash.new
  @rw_locks[title] ||= { lock: Concurrent::ReentrantReadWriteLock.new }
  return @rw_locks[title] unless version

  @rw_locks[title][version] ||= Concurrent::ReentrantReadWriteLock.new
end