Class: MasterLock::Registry
- Inherits:
-
Object
- Object
- MasterLock::Registry
- Defined in:
- lib/master_lock/registry.rb
Overview
When MasterLock acquires a lock, it registers it with a global registry. MasterLock will periodically renew all locks that are registered as long as the thread that acquired the lock is still alive and has not explicitly released the lock yet. If there is a failure to renew the lock, MasterLock identifies the lock as having already been released.
Defined Under Namespace
Classes: Registration
Instance Attribute Summary collapse
-
#locks ⇒ Array<Registration>
readonly
Currently registered locks.
Instance Method Summary collapse
-
#extend_locks ⇒ Object
Extend all currently registered locks that have been held longer than the extend_interval since they were last acquired/extended.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#register(lock, extend_interval) ⇒ Registration
Register a lock to be renewed every extend_interval seconds.
-
#unregister(registration) ⇒ Object
Unregister a lock that has been registered.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
20 21 22 23 |
# File 'lib/master_lock/registry.rb', line 20 def initialize @locks = [] @locks_mutex = Mutex.new end |
Instance Attribute Details
#locks ⇒ Array<Registration> (readonly)
Returns currently registered locks.
18 19 20 |
# File 'lib/master_lock/registry.rb', line 18 def locks @locks end |
Instance Method Details
#extend_locks ⇒ Object
Extend all currently registered locks that have been held longer than the extend_interval since they were last acquired/extended. If any locks have expired (should not happen), it will release them.
58 59 60 61 62 63 64 65 |
# File 'lib/master_lock/registry.rb', line 58 def extend_locks # Make a local copy of the locks array to avoid accessing it outside of the mutex. locks_copy = @locks_mutex.synchronize { locks.dup } locks_copy.each { |registration| extend_lock(registration) } @locks_mutex.synchronize do locks.delete_if(&:released) end end |
#register(lock, extend_interval) ⇒ Registration
Register a lock to be renewed every extend_interval seconds.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/master_lock/registry.rb', line 31 def register(lock, extend_interval) registration = Registration.new registration.lock = lock registration.mutex = Mutex.new registration.thread = Thread.current registration.acquired_at = Time.now registration.extend_interval = extend_interval registration.released = false @locks_mutex.synchronize do locks << registration end registration end |
#unregister(registration) ⇒ Object
Unregister a lock that has been registered.
49 50 51 52 53 |
# File 'lib/master_lock/registry.rb', line 49 def unregister(registration) registration.mutex.synchronize do registration.released = true end end |