2016-05-30 17:13:42 -04: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 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-05-30 17:13:42 -04:00
|
|
|
MAILROOM_NAMESPACE = 'mail_room:gitlab'
|
|
|
|
DEFAULT_REDIS_URL = 'redis://localhost:6379'
|
2016-03-09 09:16:46 -05:00
|
|
|
|
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.
|
2016-05-30 17:13:42 -04:00
|
|
|
PARAMS_MUTEX = Mutex.new
|
2016-03-18 10:31:53 -04:00
|
|
|
POOL_MUTEX = Mutex.new
|
2016-05-30 17:13:42 -04:00
|
|
|
private_constant :PARAMS_MUTEX, :POOL_MUTEX
|
2016-03-18 10:31:53 -04:00
|
|
|
|
2016-05-30 17:13:42 -04:00
|
|
|
class << self
|
|
|
|
def params
|
2016-08-05 21:01:52 -04:00
|
|
|
@params || PARAMS_MUTEX.synchronize { @params = new.params }
|
2016-05-30 17:13:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @deprecated Use .params instead to get sentinel support
|
|
|
|
def url
|
2016-07-09 11:13:19 -04:00
|
|
|
new.url
|
2016-05-30 17:13:42 -04:00
|
|
|
end
|
2016-03-16 13:10:03 -04:00
|
|
|
|
2016-05-30 17:13:42 -04:00
|
|
|
def with
|
|
|
|
if @pool.nil?
|
|
|
|
POOL_MUTEX.synchronize do
|
|
|
|
@pool = ConnectionPool.new { ::Redis.new(params) }
|
|
|
|
end
|
2016-03-18 10:31:53 -04:00
|
|
|
end
|
2016-05-30 17:13:42 -04:00
|
|
|
@pool.with { |redis| yield redis }
|
2016-03-18 10:31:53 -04:00
|
|
|
end
|
2016-08-05 21:01:52 -04:00
|
|
|
|
|
|
|
def reset_params!
|
|
|
|
@params = nil
|
|
|
|
end
|
2016-03-09 09:16:46 -05:00
|
|
|
end
|
2016-05-10 22:58:06 -04:00
|
|
|
|
2016-08-04 12:55:24 -04:00
|
|
|
def initialize(rails_env = nil)
|
2016-07-07 21:33:37 -04:00
|
|
|
@rails_env = rails_env || ::Rails.env
|
2016-05-30 17:13:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
redis_store_options
|
|
|
|
end
|
|
|
|
|
2016-07-09 11:13:19 -04:00
|
|
|
def url
|
|
|
|
raw_config_hash[:url]
|
|
|
|
end
|
|
|
|
|
2016-05-30 17:13:42 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def redis_store_options
|
|
|
|
config = raw_config_hash
|
2016-07-12 11:10:03 -04:00
|
|
|
redis_url = config.delete(:url)
|
|
|
|
redis_uri = URI.parse(redis_url)
|
2016-05-30 17:13:42 -04:00
|
|
|
|
2016-03-09 09:16:46 -05:00
|
|
|
if redis_uri.scheme == 'unix'
|
2016-05-30 17:13:42 -04:00
|
|
|
# Redis::Store does not handle Unix sockets well, so let's do it for them
|
|
|
|
config[:path] = redis_uri.path
|
2016-07-12 11:10:03 -04:00
|
|
|
config
|
2016-05-30 17:13:42 -04:00
|
|
|
else
|
2016-07-12 11:10:03 -04: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 09:16:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-30 17:13:42 -04:00
|
|
|
def raw_config_hash
|
|
|
|
config_data = fetch_config
|
2016-05-10 22:58:06 -04:00
|
|
|
|
2016-05-30 17:13:42 -04: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 09:16:46 -05:00
|
|
|
end
|
|
|
|
end
|
2016-05-30 17:13:42 -04:00
|
|
|
|
|
|
|
def fetch_config
|
2016-07-07 21:33:37 -04:00
|
|
|
file = config_file
|
|
|
|
File.exist?(file) ? YAML.load_file(file)[@rails_env] : false
|
2016-05-30 17:13:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def config_file
|
|
|
|
File.expand_path('../../../config/resque.yml', __FILE__)
|
|
|
|
end
|
2016-03-09 09:16:46 -05:00
|
|
|
end
|
2016-03-09 09:56:44 -05:00
|
|
|
end
|