Skip to content

Commit

Permalink
Little refactoring of locking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 22, 2020
1 parent 4075698 commit ca19abe
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(servers = DEFAULT_REDIS_URLS, options = {})
RedisInstance.new(server)
end
end
@quorum = (servers.length / 2).to_i + 1
@quorum = servers.length / 2 + 1
@retry_count = options[:retry_count] || DEFAULT_RETRY_COUNT
@retry_delay = options[:retry_delay] || DEFAULT_RETRY_DELAY
@retry_jitter = options[:retry_jitter] || DEFAULT_RETRY_JITTER
Expand Down Expand Up @@ -264,8 +264,9 @@ def lock_instances(resource, ttl, options)
value = (options[:extend] || { value: SecureRandom.uuid })[:value]
allow_new_lock = options[:extend_only_if_locked] ? 'no' : 'yes'

locked, time_elapsed = timed do
@servers.select { |s| s.lock resource, value, ttl, allow_new_lock }.size
locked = 0
time_elapsed = timed do
locked = @servers.count { |s| s.lock(resource, value, ttl, allow_new_lock) }
end

validity = ttl - time_elapsed - drift(ttl)
Expand Down Expand Up @@ -317,8 +318,9 @@ def drift(ttl)
end

def timed
start_time = @time_source.call()
[yield, @time_source.call() - start_time]
start_time = @time_source.call
yield
@time_source.call - start_time
end
end
end

0 comments on commit ca19abe

Please sign in to comment.