lib/lusnoc/mutex.rb in lusnoc-0.1.0 vs lib/lusnoc/mutex.rb in lusnoc-0.1.2.16548

- old
+ new

@@ -32,22 +32,20 @@ def on_mutex_lost(&block) @on_mutex_lost = block end def synchronize(timeout: 0, &block) - timeouter = Timeouter.new(timeout, - exception_class: TimeoutError, - exception_message: 'mutex acquisition expired') + t = Timeouter::Timer.new(timeout, eclass: TimeoutError, message: 'mutex acquisition expired') Session.new("mutex_session/#{key}", ttl: @ttl) do |session| @session = session session.on_session_die do @owner = nil @on_mutex_lost&.call(self) end - return acquisition_loop! key, session, value, timeouter, &block + return acquisition_loop! key, session, value, t, &block ensure release(key, session.id, timeout: 2) rescue nil logger.info("Lock #{key} released for session #{session.name}[#{session.id}]") @owner = nil @session = nil @@ -68,17 +66,17 @@ def release(key, session) Lusnoc.http_put(build_url("/v1/kv/#{key}?release=#{session.id}"), timeout: 1) end - def acquisition_loop!(key, session, value, timeouter) + def acquisition_loop!(key, session, value, t) return yield(self) if acquire(key, session, value) logger.debug("Start #{key} acquisition loop for session #{session.name}[#{session.id}]") - timeouter.loop! do + t.loop! do session.alive!(TimeoutError) - wait_for_key_released(key, timeouter.left) + wait_for_key_released(key, t.left) return yield(self) if acquire(key, session, value) logger.debug("Lock #{key} acquisition failed for session #{session.name}[#{session.id}]") sleep 1 @@ -86,12 +84,12 @@ end def wait_for_key_released(key, timeout = nil) logger.debug "Waiting for key #{key} to be fre of any session" Lusnoc::Watcher.new(build_url("/v1/kv/#{key}"), - timeout: timeout, - exception_class: TimeoutError, - exception_message: 'mutex acquisition expired').run do |body| + timeout: timeout, + eclass: TimeoutError, + emessage: 'mutex acquisition expired').run do |body| result = JSON.parse(body.empty? ? '[{}]' : body) return true if result.first['Session'].nil? end end