Module: Sidekiq::RedisConnection

Defined in:
lib/sidekiq/redis_connection.rb

Class Method Summary collapse

Class Method Details

.create(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq/redis_connection.rb', line 10

def create(options = {})
  symbolized_options = deep_symbolize_keys(options)
  symbolized_options[:url] ||= determine_redis_provider

  logger = symbolized_options.delete(:logger)
  logger&.info { "Sidekiq #{Sidekiq::VERSION} connecting to Redis with options #{scrub(symbolized_options)}" }

  raise "Sidekiq 7+ does not support Redis protocol 2" if symbolized_options[:protocol] == 2

  safe = !!symbolized_options.delete(:cluster_safe)
  raise ":nodes not allowed, Sidekiq is not safe to run on Redis Cluster" if !safe && symbolized_options.key?(:nodes)

  size = symbolized_options.delete(:size) || 5
  pool_timeout = symbolized_options.delete(:pool_timeout) || 1
  pool_name = symbolized_options.delete(:pool_name)

  # Default timeout in redis-client is 1 second, which can be too aggressive
  # if the Sidekiq process is CPU-bound. With 10-15 threads and a thread quantum of 100ms,
  # it can be easy to get the occasional ReadTimeoutError. You can still provide
  # a smaller timeout explicitly:
  #     config.redis = { url: "...", timeout: 1 }
  symbolized_options[:timeout] ||= 3

  redis_config = Sidekiq::RedisClientAdapter.new(symbolized_options)
  ConnectionPool.new(timeout: pool_timeout, size: size, name: pool_name) do
    redis_config.new_client
  end
end