Class: DistributedMutex
- Inherits:
-
Mutex
- Object
- Mutex
- DistributedMutex
- Defined in:
- lib/distributed_mutex.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_TIMEOUT =
1
- DEFAULT_EXCEPTION_ON_TIMEOUT =
false
Instance Attribute Summary collapse
-
#exception_on_timeout ⇒ Object
readonly
Returns the value of attribute exception_on_timeout.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #excluse_unlock ⇒ Object
-
#initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT) ⇒ DistributedMutex
constructor
A new instance of DistributedMutex.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #synchronize(&block) ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT) ⇒ DistributedMutex
Returns a new instance of DistributedMutex.
12 13 14 15 16 17 |
# File 'lib/distributed_mutex.rb', line 12 def initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT) @key = key @timeout = timeout @locked = false @exception_on_timeout = exception_on_timeout end |
Instance Attribute Details
#exception_on_timeout ⇒ Object (readonly)
Returns the value of attribute exception_on_timeout.
9 10 11 |
# File 'lib/distributed_mutex.rb', line 9 def exception_on_timeout @exception_on_timeout end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/distributed_mutex.rb', line 9 def key @key end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/distributed_mutex.rb', line 9 def timeout @timeout end |
Class Method Details
.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, &block) ⇒ Object
73 74 75 76 |
# File 'lib/distributed_mutex.rb', line 73 def self.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, &block) mutex = new(key, timeout, exception_on_timeout) mutex.synchronize(&block) end |
Instance Method Details
#excluse_unlock ⇒ Object
10 |
# File 'lib/distributed_mutex.rb', line 10 alias excluse_unlock unlock |
#lock ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/distributed_mutex.rb', line 19 def lock @locked = get_lock if true == @locked true else if @exception_on_timeout raise MutexLockTimeout.new else false end end end |
#locked? ⇒ Boolean
32 33 34 |
# File 'lib/distributed_mutex.rb', line 32 def locked? @locked end |
#synchronize(&block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/distributed_mutex.rb', line 36 def synchronize(&block) if self.lock begin yield ensure self.unlock end true else false end end |
#try_lock ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/distributed_mutex.rb', line 49 def try_lock was_locked = nil begin self.lock was_locked = locked? ensure self.unlock end was_locked end |
#unlock ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/distributed_mutex.rb', line 60 def unlock if locked? if release_lock @locked = false true else raise MutexLockReleaseFailure end else false end end |