Skip to content

Commit

Permalink
Merge pull request #146 from karrocon/feature/support-rails-7.1
Browse files Browse the repository at this point in the history
Stop monkey patching ::Redis
  • Loading branch information
leandromoreira authored Oct 19, 2023
2 parents 2f4e406 + 818a28d commit 65394fc
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 65394fc

Please sign in to comment.