Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little refactoring of locking logic #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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: 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@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