diff --git a/lib/sidekiq/redis_connection.rb b/lib/sidekiq/redis_connection.rb index c029af3a..e06fa221 100644 --- a/lib/sidekiq/redis_connection.rb +++ b/lib/sidekiq/redis_connection.rb @@ -9,19 +9,19 @@ module Sidekiq url = options[:url] || determine_redis_provider || 'redis://localhost:6379/0' # need a connection for Fetcher and Retry size = options[:size] || (Sidekiq.server? ? (Sidekiq.options[:concurrency] + 2) : 5) - timeout = options[:timeout] || 1 + pool_timeout = options[:pool_timeout] || 1 log_info(url, options) - ConnectionPool.new(:timeout => timeout, :size => size) do - build_client(url, options[:namespace], options[:driver] || 'ruby') + ConnectionPool.new(:timeout => pool_timeout, :size => size) do + build_client(url, options[:namespace], options[:driver] || 'ruby', options[:network_timeout]) end end private - def build_client(url, namespace, driver) - client = Redis.connect(:url => url, :driver => driver) + def build_client(url, namespace, driver, network_timeout) + client = Redis.connect client_opts(url, driver, network_timeout) if namespace require 'redis/namespace' Redis::Namespace.new(namespace, :redis => client) @@ -30,6 +30,14 @@ module Sidekiq end end + def client_opts(url, driver, timeout) + if timeout + { :url => url, :driver => driver, :timeout => timeout } + else + { :url => url, :driver => driver } + end + end + def log_info(url, options) opts = options.dup opts.delete(:url) diff --git a/test/test_redis_connection.rb b/test/test_redis_connection.rb index cb6de0a7..9bf4099f 100644 --- a/test/test_redis_connection.rb +++ b/test/test_redis_connection.rb @@ -10,6 +10,22 @@ class TestRedisConnection < Minitest::Test assert_equal Redis, pool.checkout.class end + describe "network_timeout" do + it "sets a custom network_timeout if specified" do + pool = Sidekiq::RedisConnection.create(:network_timeout => 8) + redis = pool.checkout + + assert_equal 8, redis.client.timeout + end + + it "uses the default network_timeout if none specified" do + pool = Sidekiq::RedisConnection.create + redis = pool.checkout + + assert_equal 5, redis.client.timeout + end + end + describe "namespace" do it "uses a given :namespace" do pool = Sidekiq::RedisConnection.create(:namespace => "xxx") @@ -23,9 +39,9 @@ class TestRedisConnection < Minitest::Test end end - describe "timeout" do + describe "pool_timeout" do it "uses a given :timeout over the default of 1" do - pool = Sidekiq::RedisConnection.create(:timeout => 5) + pool = Sidekiq::RedisConnection.create(:pool_timeout => 5) assert_equal 5, pool.instance_eval{ @timeout } end