Skip to content

Commit

Permalink
Stop monkey patching ::Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
karrocon committed Oct 19, 2023
1 parent 2f4e406 commit 818a28d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
15 changes: 5 additions & 10 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 818a28d

Please sign in to comment.