From ca19abedb1c8d0dd5bf049f81d7377b8a043ddfb Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 6 Feb 2020 16:05:51 +0200 Subject: [PATCH] Little refactoring of locking logic --- lib/redlock/client.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/redlock/client.rb b/lib/redlock/client.rb index cabf640..49694aa 100644 --- a/lib/redlock/client.rb +++ b/lib/redlock/client.rb @@ -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 @@ -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) @@ -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