Commit 52ee85e7 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Initialize Redis pool in single-threaded context

This side-steps the need for mutexes and whatnot.
parent 7d48778c
# Make sure we initialize a Redis connection pool before Sidekiq starts
# multi-threaded execution.
Gitlab::Redis.with { nil }
...@@ -11,13 +11,6 @@ module Gitlab ...@@ -11,13 +11,6 @@ module Gitlab
DEFAULT_REDIS_URL = 'redis://localhost:6379' DEFAULT_REDIS_URL = 'redis://localhost:6379'
CONFIG_FILE = File.expand_path('../../config/resque.yml', __dir__) CONFIG_FILE = File.expand_path('../../config/resque.yml', __dir__)
# To be thread-safe we must be careful when writing the class instance
# variables @_raw_config and @pool. Because @pool depends on @_raw_config we need two
# mutexes to prevent deadlock.
RAW_CONFIG_MUTEX = Mutex.new
POOL_MUTEX = Mutex.new
private_constant :RAW_CONFIG_MUTEX, :POOL_MUTEX
class << self class << self
# Do NOT cache in an instance variable. Result may be mutated by caller. # Do NOT cache in an instance variable. Result may be mutated by caller.
def params def params
...@@ -31,24 +24,19 @@ module Gitlab ...@@ -31,24 +24,19 @@ module Gitlab
end end
def with def with
if @pool.nil? @pool ||= ConnectionPool.new { ::Redis.new(params) }
POOL_MUTEX.synchronize do
@pool = ConnectionPool.new { ::Redis.new(params) }
end
end
@pool.with { |redis| yield redis } @pool.with { |redis| yield redis }
end end
def _raw_config def _raw_config
return @_raw_config if defined?(@_raw_config) return @_raw_config if defined?(@_raw_config)
RAW_CONFIG_MUTEX.synchronize do begin
begin @_raw_config = File.read(CONFIG_FILE).freeze
@_raw_config = File.read(CONFIG_FILE).freeze rescue Errno::ENOENT
rescue Errno::ENOENT @_raw_config = false
@_raw_config = false
end
end end
@_raw_config @_raw_config
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment