From 818a28df2e5590cc52a9758034a9ec63ab8f1d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Rodr=C3=ADguez=20Contreras?= Date: Wed, 18 Oct 2023 15:30:04 +0200 Subject: [PATCH] Stop monkey patching ::Redis --- lib/redlock/client.rb | 15 +++++---------- spec/client_spec.rb | 6 +----- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/redlock/client.rb b/lib/redlock/client.rb index 9519f7f..e829711 100644 --- a/lib/redlock/client.rb +++ b/lib/redlock/client.rb @@ -157,24 +157,19 @@ def valid_lock?(lock_info) private class RedisInstance - module ConnectionPoolLike - def with - yield self - end - end - def initialize(connection) @monitor = Monitor.new - if connection.respond_to?(:with) + if connection.respond_to?(:call) @redis = connection else if connection.respond_to?(:client) @redis = connection - else + elsif connection.respond_to?(:key?) @redis = initialize_client(connection) + else + @redis = connection end - @redis.extend(ConnectionPoolLike) end end @@ -202,7 +197,7 @@ def initialize_client(options) end def synchronize - @monitor.synchronize { @redis.with { |connection| yield(connection) } } + @monitor.synchronize { @redis.then { |connection| yield(connection) } } end def lock(resource, val, ttl, allow_new_lock) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index d061dbe..cbefc40 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -30,11 +30,7 @@ let(:redis3_host) { ENV["REDIS3_HOST"] || "127.0.0.1" } let(:redis3_port) { ENV["REDIS3_PORT"] || "6379" } let(:unreachable_redis) { - redis = RedisClient.new(url: 'redis://localhost:46864') - def redis.with - yield self - end - redis + RedisClient.new(url: 'redis://localhost:46864') } describe 'initialize' do