From bbf637537d9c13b5b3040848c110c75750ce6113 Mon Sep 17 00:00:00 2001 From: Pelle ten Cate Date: Wed, 8 May 2019 17:11:04 -0500 Subject: [PATCH] Add Timeout exception and raise it when a lock could not be acquired --- lib/redis/semaphore.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/redis/semaphore.rb b/lib/redis/semaphore.rb index 461fa72..ab32ca3 100644 --- a/lib/redis/semaphore.rb +++ b/lib/redis/semaphore.rb @@ -5,6 +5,8 @@ class Semaphore EXISTS_TOKEN = "1" API_VERSION = "1" + class Timeout < StandardError; end + # stale_client_timeout is the threshold of time before we assume # that something has gone terribly wrong with a client and we # invalidate it's lock. @@ -56,7 +58,7 @@ def delete! @redis.del(version_key) end - def lock(timeout = nil) + def lock(timeout = nil, raise_on_timeout: false) exists_or_create! release_stale_locks! if check_staleness? @@ -67,7 +69,11 @@ def lock(timeout = nil) current_token = @redis.lpop(available_key) end - return false if current_token.nil? + if current_token.nil? + raise Timeout, "Could not acquire lock on semaphonre #{@name} within #{timeout} seconds" if raise_on_timeout + + return false + end @tokens.push(current_token) @redis.hset(grabbed_key, current_token, current_time.to_f) @@ -244,7 +250,7 @@ def current_time begin instant = redis_namespace? ? @redis.redis.time : @redis.time Time.at(instant[0], instant[1]) - rescue + rescue StandardError @use_local_time = true current_time end