2015-12-31 18:33:35 -05:00
# frozen_string_literal: true
2012-10-14 19:54:34 -04:00
require 'connection_pool'
2012-03-31 15:45:24 -04:00
require 'redis'
2013-11-17 10:20:28 -05:00
require 'uri'
2012-02-08 17:43:57 -05:00
module Sidekiq
class RedisConnection
2013-04-17 13:07:46 -04:00
class << self
2012-04-06 12:43:02 -04:00
2013-04-17 13:07:46 -04:00
def create ( options = { } )
2017-05-13 01:03:32 -04:00
options . keys . each do | key |
options [ key . to_sym ] = options . delete ( key )
end
2016-05-26 01:50:37 -04:00
2017-06-21 07:54:18 -04:00
options [ :id ] = " Sidekiq- #{ Sidekiq . server? ? " server " : " client " } -PID- #{ $$ } " if ! options . has_key? ( :id )
2015-04-04 20:17:13 -04:00
options [ :url ] || = determine_redis_provider
2013-11-24 09:16:37 -05:00
2015-10-30 11:43:56 -04:00
size = options [ :size ] || ( Sidekiq . server? ? ( Sidekiq . options [ :concurrency ] + 5 ) : 5 )
2013-04-17 13:07:46 -04:00
2015-10-21 00:25:03 -04:00
verify_sizing ( size , Sidekiq . options [ :concurrency ] ) if Sidekiq . server?
pool_timeout = options [ :pool_timeout ] || 1
2013-10-21 14:28:38 -04:00
log_info ( options )
2013-04-17 13:07:46 -04:00
2013-07-17 11:02:51 -04:00
ConnectionPool . new ( :timeout = > pool_timeout , :size = > size ) do
2013-10-21 14:28:38 -04:00
build_client ( options )
2013-04-17 13:07:46 -04:00
end
end
private
2015-10-21 00:25:03 -04:00
# Sidekiq needs a lot of concurrent Redis connections.
#
# We need a connection for each Processor.
# We need a connection for Pro's real-time change listener
# We need a connection to various features to call Redis every few seconds:
# - the process heartbeat.
# - enterprise's leader election
# - enterprise's cron support
def verify_sizing ( size , concurrency )
2017-11-07 12:10:06 -05:00
raise ArgumentError , " Your Redis connection pool is too small for Sidekiq to work. Your pool has #{ size } connections but must have at least #{ concurrency + 2 } " if size < = concurrency
2015-10-21 00:25:03 -04:00
end
2013-10-21 14:28:38 -04:00
def build_client ( options )
namespace = options [ :namespace ]
client = Redis . new client_opts ( options )
2013-04-17 13:07:46 -04:00
if namespace
2015-10-21 00:25:03 -04:00
begin
require 'redis/namespace'
Redis :: Namespace . new ( namespace , :redis = > client )
rescue LoadError
2016-05-26 01:50:37 -04:00
Sidekiq . logger . error ( " Your Redis configuration uses the namespace ' #{ namespace } ' but the redis-namespace gem is not included in the Gemfile. " \
" Add the gem to your Gemfile to continue using a namespace. Otherwise, remove the namespace parameter. " )
2015-10-21 00:25:03 -04:00
exit ( - 127 )
end
2013-04-17 13:07:46 -04:00
else
client
end
end
2013-10-21 14:28:38 -04:00
def client_opts ( options )
2013-10-21 15:36:51 -04:00
opts = options . dup
if opts [ :namespace ]
opts . delete ( :namespace )
2013-07-17 11:02:51 -04:00
end
2013-10-21 14:28:38 -04:00
2013-10-21 15:36:51 -04:00
if opts [ :network_timeout ]
opts [ :timeout ] = opts [ :network_timeout ]
opts . delete ( :network_timeout )
2013-10-21 14:28:38 -04:00
end
2018-02-16 16:01:25 -05:00
opts [ :driver ] || = 'ruby'
2017-01-13 11:19:31 -05:00
# Issue #3303, redis-rb will silently retry an operation.
# This can lead to duplicate jobs if Sidekiq::Client's LPUSH
# is performed twice but I believe this is much, much rarer
# than the reconnect silently fixing a problem; we keep it
# on by default.
opts [ :reconnect_attempts ] || = 1
2013-10-21 14:28:38 -04:00
2013-10-21 15:36:51 -04:00
opts
2013-07-17 11:02:51 -04:00
end
2013-10-21 14:28:38 -04:00
def log_info ( options )
2013-11-17 10:20:28 -05:00
# Don't log Redis AUTH password
2014-06-20 04:12:53 -04:00
redacted = " REDACTED "
2013-11-17 10:20:28 -05:00
scrubbed_options = options . dup
if scrubbed_options [ :url ] && ( uri = URI . parse ( scrubbed_options [ :url ] ) ) && uri . password
2014-06-20 04:12:53 -04:00
uri . password = redacted
2013-11-17 10:20:28 -05:00
scrubbed_options [ :url ] = uri . to_s
end
2014-06-20 04:12:53 -04:00
if scrubbed_options [ :password ]
scrubbed_options [ :password ] = redacted
end
2013-04-17 13:07:46 -04:00
if Sidekiq . server?
2013-11-17 10:20:28 -05:00
Sidekiq . logger . info ( " Booting Sidekiq #{ Sidekiq :: VERSION } with redis options #{ scrubbed_options } " )
2013-04-17 13:07:46 -04:00
else
2014-06-22 00:01:59 -04:00
Sidekiq . logger . debug ( " #{ Sidekiq :: NAME } client with redis options #{ scrubbed_options } " )
2013-04-17 13:07:46 -04:00
end
2013-04-17 12:53:01 -04:00
end
2013-04-17 13:07:46 -04:00
def determine_redis_provider
2017-06-19 10:42:00 -04:00
# If you have this in your environment:
# MY_REDIS_URL=redis://hostname.example.com:1238/4
# then set:
# REDIS_PROVIDER=MY_REDIS_URL
# and Sidekiq will find your custom URL variable with no custom
# initialization code at all.
ENV [
ENV [ 'REDIS_PROVIDER' ] || 'REDIS_URL'
]
2012-03-14 00:19:46 -04:00
end
2012-02-08 17:43:57 -05:00
2012-10-14 17:58:20 -04:00
end
2012-02-08 17:43:57 -05:00
end
end