2016-05-30 21:13:42 +00:00
|
|
|
# This file should not have any direct dependency on Rails environment
|
|
|
|
# please require all dependencies below:
|
|
|
|
require 'active_support/core_ext/hash/keys'
|
|
|
|
|
2016-03-09 14:16:46 +00:00
|
|
|
module Gitlab
|
2016-03-16 17:10:03 +00:00
|
|
|
class Redis
|
2016-04-07 13:42:07 +00:00
|
|
|
CACHE_NAMESPACE = 'cache:gitlab'
|
2016-04-13 10:20:57 +00:00
|
|
|
SESSION_NAMESPACE = 'session:gitlab'
|
|
|
|
SIDEKIQ_NAMESPACE = 'resque:gitlab'
|
2016-05-30 21:13:42 +00:00
|
|
|
MAILROOM_NAMESPACE = 'mail_room:gitlab'
|
|
|
|
DEFAULT_REDIS_URL = 'redis://localhost:6379'
|
2016-03-09 14:16:46 +00:00
|
|
|
|
2016-03-18 14:31:53 +00: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.
|
2016-05-30 21:13:42 +00:00
|
|
|
PARAMS_MUTEX = Mutex.new
|
2016-03-18 14:31:53 +00:00
|
|
|
POOL_MUTEX = Mutex.new
|
2016-05-30 21:13:42 +00:00
|
|
|
private_constant :PARAMS_MUTEX, :POOL_MUTEX
|
2016-03-18 14:31:53 +00:00
|
|
|
|
2016-05-30 21:13:42 +00:00
|
|
|
class << self
|
|
|
|
def params
|
2016-08-06 01:01:52 +00:00
|
|
|
@params || PARAMS_MUTEX.synchronize { @params = new.params }
|
2016-05-30 21:13:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @deprecated Use .params instead to get sentinel support
|
|
|
|
def url
|
2016-07-09 15:13:19 +00:00
|
|
|
new.url
|
2016-05-30 21:13:42 +00:00
|
|
|
end
|
2016-03-16 17:10:03 +00:00
|
|
|
|
2016-05-30 21:13:42 +00:00
|
|
|
def with
|
|
|
|
if @pool.nil?
|
|
|
|
POOL_MUTEX.synchronize do
|
|
|
|
@pool = ConnectionPool.new { ::Redis.new(params) }
|
|
|
|
end
|
2016-03-18 14:31:53 +00:00
|
|
|
end
|
2016-05-30 21:13:42 +00:00
|
|
|
@pool.with { |redis| yield redis }
|
2016-03-18 14:31:53 +00:00
|
|
|
end
|
2016-08-06 01:01:52 +00:00
|
|
|
|
|
|
|
def reset_params!
|
|
|
|
@params = nil
|
|
|
|
end
|
2016-03-09 14:16:46 +00:00
|
|
|
end
|
2016-05-11 02:58:06 +00:00
|
|
|
|
2016-08-04 16:55:24 +00:00
|
|
|
def initialize(rails_env = nil)
|
2016-07-08 01:33:37 +00:00
|
|
|
@rails_env = rails_env || ::Rails.env
|
2016-05-30 21:13:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
redis_store_options
|
|
|
|
end
|
|
|
|
|
2016-07-09 15:13:19 +00:00
|
|
|
def url
|
|
|
|
raw_config_hash[:url]
|
|
|
|
end
|
|
|
|
|
2016-05-30 21:13:42 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def redis_store_options
|
|
|
|
config = raw_config_hash
|
2016-07-12 15:10:03 +00:00
|
|
|
redis_url = config.delete(:url)
|
|
|
|
redis_uri = URI.parse(redis_url)
|
2016-05-30 21:13:42 +00:00
|
|
|
|
2016-03-09 14:16:46 +00:00
|
|
|
if redis_uri.scheme == 'unix'
|
2016-05-30 21:13:42 +00:00
|
|
|
# Redis::Store does not handle Unix sockets well, so let's do it for them
|
|
|
|
config[:path] = redis_uri.path
|
2016-07-12 15:10:03 +00:00
|
|
|
config
|
2016-05-30 21:13:42 +00:00
|
|
|
else
|
2016-07-12 15:10:03 +00:00
|
|
|
redis_hash = ::Redis::Store::Factory.extract_host_options_from_uri(redis_url)
|
|
|
|
# order is important here, sentinels must be after the connection keys.
|
|
|
|
# {url: ..., port: ..., sentinels: [...]}
|
|
|
|
redis_hash.merge(config)
|
2016-03-09 14:16:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-30 21:13:42 +00:00
|
|
|
def raw_config_hash
|
|
|
|
config_data = fetch_config
|
2016-05-11 02:58:06 +00:00
|
|
|
|
2016-05-30 21:13:42 +00:00
|
|
|
if config_data
|
|
|
|
config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
|
|
|
|
else
|
|
|
|
{ url: DEFAULT_REDIS_URL }
|
2016-03-09 14:16:46 +00:00
|
|
|
end
|
|
|
|
end
|
2016-05-30 21:13:42 +00:00
|
|
|
|
|
|
|
def fetch_config
|
2016-07-08 01:33:37 +00:00
|
|
|
file = config_file
|
|
|
|
File.exist?(file) ? YAML.load_file(file)[@rails_env] : false
|
2016-05-30 21:13:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def config_file
|
|
|
|
File.expand_path('../../../config/resque.yml', __FILE__)
|
|
|
|
end
|
2016-03-09 14:16:46 +00:00
|
|
|
end
|
2016-03-09 14:56:44 +00:00
|
|
|
end
|