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

Re-raise Redis connection errors in #lock #110

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.with { |conn| conn.evalsha Scripts::LOCK_SCRIPT_SHA, keys: [resource], argv: [val, ttl, allow_new_lock] }
end
rescue Redis::BaseConnectionError
false
end

def unlock(resource, val)
Expand Down
12 changes: 7 additions & 5 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ def redis.with
end

context 'when a server goes away' do
it 'does not raise an error on connection issues' do
# We re-route the lock manager to a (hopefully) non-existent Redis URL.
it 'raises an error on connection issues' do
# Set lock manager to a (hopefully) non-existent Redis URL to test error
redis_instance = lock_manager.instance_variable_get(:@servers).first
redis_instance.instance_variable_set(:@redis, unreachable_redis)

expect {
expect(lock_manager.lock(resource_key, ttl)).to be_falsey
}.to_not raise_error
lock_manager.lock(resource_key, ttl)
}.to raise_error(Redis::CannotConnectError)
end
end

Expand All @@ -259,7 +259,9 @@ def redis.with
redis_instance = lock_manager.instance_variable_get(:@servers).first
old_redis = redis_instance.instance_variable_get(:@redis)
redis_instance.instance_variable_set(:@redis, unreachable_redis)
expect(lock_manager.lock(resource_key, ttl)).to be_falsey
expect {
lock_manager.lock(resource_key, ttl)
}.to raise_error(Redis::CannotConnectError)
redis_instance.instance_variable_set(:@redis, old_redis)
expect(lock_manager.lock(resource_key, ttl)).to be_truthy
end
Expand Down