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

Merge pull request #1062 from LeakyBucket/master

Allow the configuration of the Redis Connection Pool Timeout
This commit is contained in:
Mike Perham 2013-07-17 09:50:00 -07:00
commit b62d8a3ac2
3 changed files with 45 additions and 4 deletions

2
.gitignore vendored
View file

@ -6,4 +6,6 @@ Gemfile.lock
dump.rdb
.rbx
coverage/
vendor/
.bundle/
.sass-cache/

View file

@ -9,18 +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)
pool_timeout = options[:pool_timeout] || 1
log_info(url, options)
ConnectionPool.new(:timeout => 1, :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)
@ -29,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")
@ -22,6 +38,20 @@ class TestRedisConnection < Minitest::Test
assert_equal "yyy", pool.checkout.namespace
end
end
describe "pool_timeout" do
it "uses a given :timeout over the default of 1" do
pool = Sidekiq::RedisConnection.create(:pool_timeout => 5)
assert_equal 5, pool.instance_eval{ @timeout }
end
it "uses the default timeout of 1 if no override" do
pool = Sidekiq::RedisConnection.create
assert_equal 1, pool.instance_eval{ @timeout }
end
end
end
describe ".determine_redis_provider" do