2016-03-09 09:16:46 -05:00
|
|
|
module Gitlab
|
2016-03-16 13:10:03 -04:00
|
|
|
class Redis
|
2016-04-07 09:42:07 -04:00
|
|
|
CACHE_NAMESPACE = 'cache:gitlab'
|
2016-04-13 06:20:57 -04:00
|
|
|
SESSION_NAMESPACE = 'session:gitlab'
|
|
|
|
SIDEKIQ_NAMESPACE = 'resque:gitlab'
|
2016-04-07 09:42:07 -04:00
|
|
|
|
2016-03-09 09:16:46 -05:00
|
|
|
attr_reader :url
|
|
|
|
|
2016-03-18 10:31:53 -04:00
|
|
|
# To be thread-safe we must be careful when writing the class instance
|
|
|
|
# variables @url and @pool. Because @pool depends on @url we need two
|
|
|
|
# mutexes to prevent deadlock.
|
|
|
|
URL_MUTEX = Mutex.new
|
|
|
|
POOL_MUTEX = Mutex.new
|
|
|
|
private_constant :URL_MUTEX, :POOL_MUTEX
|
|
|
|
|
2016-03-09 09:16:46 -05:00
|
|
|
def self.url
|
2016-03-18 10:31:53 -04:00
|
|
|
@url || URL_MUTEX.synchronize { @url = new.url }
|
2016-03-16 13:10:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.with
|
2016-03-18 10:31:53 -04:00
|
|
|
if @pool.nil?
|
|
|
|
POOL_MUTEX.synchronize do
|
|
|
|
@pool = ConnectionPool.new { ::Redis.new(url: url) }
|
|
|
|
end
|
|
|
|
end
|
2016-03-16 13:10:03 -04:00
|
|
|
@pool.with { |redis| yield redis }
|
2016-03-09 09:16:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.redis_store_options
|
|
|
|
url = new.url
|
2016-03-16 13:10:03 -04:00
|
|
|
redis_config_hash = ::Redis::Store::Factory.extract_host_options_from_uri(url)
|
2016-03-09 09:16:46 -05:00
|
|
|
# Redis::Store does not handle Unix sockets well, so let's do it for them
|
|
|
|
redis_uri = URI.parse(url)
|
|
|
|
if redis_uri.scheme == 'unix'
|
|
|
|
redis_config_hash[:path] = redis_uri.path
|
|
|
|
end
|
|
|
|
redis_config_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(rails_env=nil)
|
|
|
|
rails_env ||= Rails.env
|
|
|
|
config_file = File.expand_path('../../../config/resque.yml', __FILE__)
|
|
|
|
|
|
|
|
@url = "redis://localhost:6379"
|
|
|
|
if File.exists?(config_file)
|
|
|
|
@url =YAML.load_file(config_file)[rails_env]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-03-09 09:56:44 -05:00
|
|
|
end
|