Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/redis/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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?

Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [116/80]


return false
end

@tokens.push(current_token)
@redis.hset(grabbed_key, current_token, current_time.to_f)
Expand Down Expand Up @@ -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
Expand Down