Skip to content

Commit

Permalink
Rescue Redis' CannotConnectError, the algorithm should allow it.
Browse files Browse the repository at this point in the history
Fixes #60.
  • Loading branch information
maltoe committed May 7, 2018
1 parent fd6596c commit 27dcd0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/redlock/client.rb
Expand Up @@ -112,6 +112,8 @@ def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.evalsha @lock_script_sha, keys: [resource], argv: [val, ttl, allow_new_lock]
end
rescue Redis::CannotConnectError
false
end

def unlock(resource, val)
Expand Down
12 changes: 12 additions & 0 deletions spec/client_spec.rb
Expand Up @@ -140,6 +140,18 @@
end
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.
redis_instance = lock_manager.instance_variable_get(:@servers).first
redis_instance.instance_variable_set(:@redis, Redis.new(url: 'redis://localhost:46864'))

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

context 'when script cache has been flushed' do
before(:each) do
@manipulated_instance = lock_manager.instance_variable_get(:@servers).first
Expand Down

0 comments on commit 27dcd0b

Please sign in to comment.