From da3fbed14bd802247f7cc088832a34ab637de078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=BChlmann?= Date: Sun, 9 Sep 2018 15:11:37 +0200 Subject: [PATCH] Use redis' default driver (#3956) Redis stores its loaded drivers in `Redis::Connection.drivers` and uses the last one of them when initializing a new client. Sidekiq always uses `'ruby'` (or `Redis::Connection::Ruby`) per default, though. With this commit we are following redis' default by passing the last loaded driver per default (or a given `:driver`). --- lib/sidekiq/redis_connection.rb | 2 +- test/test_redis_connection.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/sidekiq/redis_connection.rb b/lib/sidekiq/redis_connection.rb index 66d3d428..98e86442 100644 --- a/lib/sidekiq/redis_connection.rb +++ b/lib/sidekiq/redis_connection.rb @@ -78,7 +78,7 @@ module Sidekiq opts.delete(:network_timeout) end - opts[:driver] ||= 'ruby' + opts[:driver] ||= Redis::Connection.drivers.last || 'ruby' # Issue #3303, redis-rb will silently retry an operation. # This can lead to duplicate jobs if Sidekiq::Client's LPUSH diff --git a/test/test_redis_connection.rb b/test/test_redis_connection.rb index c89ba7d6..bdb05ef6 100644 --- a/test/test_redis_connection.rb +++ b/test/test_redis_connection.rb @@ -144,6 +144,37 @@ class TestRedisConnection < Sidekiq::Test assert_equal 1, pool.instance_eval{ @timeout } end end + + describe "driver" do + it "uses redis' ruby driver" do + pool = Sidekiq::RedisConnection.create + redis = pool.checkout + + assert_equal Redis::Connection::Ruby, redis.instance_variable_get(:@client).driver + end + + it "uses redis' default driver if there are many available" do + begin + redis_driver = Object.new + Redis::Connection.drivers << redis_driver + + pool = Sidekiq::RedisConnection.create + redis = pool.checkout + + assert_equal redis_driver, redis.instance_variable_get(:@client).driver + ensure + Redis::Connection.drivers.pop + end + end + + it "uses a given :driver" do + redis_driver = Object.new + pool = Sidekiq::RedisConnection.create(:driver => redis_driver) + redis = pool.checkout + + assert_equal redis_driver, redis.instance_variable_get(:@client).driver + end + end end describe ".determine_redis_provider" do