Skip to content

Commit

Permalink
Merge pull request #140 from dhruvCW/support_hash_config
Browse files Browse the repository at this point in the history
Add support for passing config objects to the client
  • Loading branch information
leandromoreira committed Aug 5, 2023
2 parents d906f75 + fc7a1d1 commit 664227b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
25 changes: 24 additions & 1 deletion lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,35 @@ def initialize(connection)
if connection.respond_to?(:client)
@redis = connection
else
@redis = RedisClient.new(connection)
@redis = initialize_client(connection)
end
@redis.extend(ConnectionPoolLike)
end
end

def initialize_client(options)
if options.key?(:sentinels)
if url = options.delete(:url)
uri = URI.parse(url)
if !options.key?(:name) && uri.host
options[:name] = uri.host
end

if !options.key?(:password) && uri.password && !uri.password.empty?
options[:password] = uri.password
end

if !options.key?(:username) && uri.user && !uri.user.empty?
options[:username] = uri.user
end
end

RedisClient.sentinel(**options).new_client
else
RedisClient.config(**options).new_client
end
end

def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.with { |conn|
Expand Down
2 changes: 1 addition & 1 deletion redlock.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'redis-client', '~> 0.14.1'
spec.add_dependency 'redis-client', '>= 0.14.1', '< 1.0.0'

spec.add_development_dependency 'connection_pool', '~> 2.2'
spec.add_development_dependency 'coveralls', '~> 0.8'
Expand Down
10 changes: 10 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def redis.with
lock_manager.unlock(lock_info)
end

it 'accepts Configuration hashes' do
config = { url: "redis://#{redis1_host}:#{redis1_port}" }
_redlock = Redlock::Client.new([config])

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

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

Expand Down

0 comments on commit 664227b

Please sign in to comment.