Skip to content

Commit

Permalink
Prioritize hash in connection parameter detection
Browse files Browse the repository at this point in the history
Allow to detect hashes responding to `#call`, like
`ActiveSupport::OrderedOptions`

This will enable out of the box compatibility with
`Rails.application.config_for` method and should not break existing
use cases

Close leandromoreira#152
  • Loading branch information
tagliala committed Nov 21, 2024
1 parent f176e57 commit c88c8ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,12 @@ class RedisInstance
def initialize(connection)
@monitor = Monitor.new

if connection.respond_to?(:call)
@redis = connection
else
if connection.respond_to?(:client)
@redis = connection
elsif connection.respond_to?(:key?)
@redis = initialize_client(connection)
@redis =
if connection.is_a?(Hash)
initialize_client(connection)
else
@redis = connection
connection
end
end
end

def initialize_client(options)
Expand Down
14 changes: 14 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@
lock_manager.unlock(lock_info)
end

it 'accepts hashes responding to call like ActiveSupport::OrderedOptions' do
callable_hash = Class.new(Hash) do
def call; end
end

config = callable_hash.new.merge(url: "redis://#{redis1_host}:#{redis1_port}")
redlock = Redlock::Client.new([config])

lock_info = redlock.lock(resource_key, ttl)
expect(lock_info).to be_a(Hash)
expect(resource_key).to_not be_lockable(lock_manager, ttl)
redlock.unlock(lock_info)
end

it 'does not load scripts' do
redis_client.call('SCRIPT', 'FLUSH')

Expand Down

0 comments on commit c88c8ce

Please sign in to comment.