class Zlk::Lock
Constants
- LockWaitTimeoutError
Attributes
path[R]
Public Class Methods
new(path)
click to toggle source
# File lib/zlk/lock.rb, line 9 def initialize(path) @path = sanitize_path(path) end
Public Instance Methods
acquirable?()
click to toggle source
# File lib/zlk/lock.rb, line 39 def acquirable? with_locker do |locker| locker.acquirable? end end
run_exclusively() { || ... }
click to toggle source
# File lib/zlk/lock.rb, line 13 def run_exclusively with_locker do |locker| locker.with_lock(wait: true) { yield } end end
with_timeout(timeout) { |true| ... }
click to toggle source
# File lib/zlk/lock.rb, line 19 def with_timeout(timeout) with_locker do |locker| begin locker.with_lock(wait: timeout) { yield(true) } rescue ZK::Exceptions::LockWaitTimeoutError => e yield(false) end end end
with_timeout!(timeout) { || ... }
click to toggle source
# File lib/zlk/lock.rb, line 29 def with_timeout!(timeout) with_timeout(timeout) do |locked| if locked yield else fail Zlk::Lock::LockWaitTimeoutError end end end
Private Instance Methods
sanitize_path(path)
click to toggle source
NOTE: it can be reimplemented really precisely (github.com/apache/zookeeper/blob/release-3.5.2/src/java/main/org/apache/zookeeper/common/PathUtils.java#L43-L102) but simple is better. Leave only word characters and substitute the rest with an underscore
# File lib/zlk/lock.rb, line 56 def sanitize_path(path) I18n.transliterate(path).scan(/\w+/).join('_') end
with_locker() { |locker| ... }
click to toggle source
# File lib/zlk/lock.rb, line 47 def with_locker Zlk.connection_pool.with_connection do |connection| locker = connection.locker(path) yield(locker) end end