1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Added option to set the network timeout for the Redis connection

This commit is contained in:
Glen 2013-07-17 09:02:51 -06:00
parent 5a4de159f0
commit 48c70f8545
2 changed files with 31 additions and 7 deletions

View file

@ -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)

View file

@ -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