debian-mirror-gitlab/lib/gitlab/redis.rb

100 lines
2.6 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
# This file should not have any direct dependency on Rails environment
# please require all dependencies below:
require 'active_support/core_ext/hash/keys'
2016-06-02 11:05:42 +05:30
module Gitlab
class Redis
CACHE_NAMESPACE = 'cache:gitlab'
SESSION_NAMESPACE = 'session:gitlab'
SIDEKIQ_NAMESPACE = 'resque:gitlab'
2016-09-13 17:45:13 +05:30
MAILROOM_NAMESPACE = 'mail_room:gitlab'
DEFAULT_REDIS_URL = 'redis://localhost:6379'
2016-11-03 12:29:30 +05:30
CONFIG_FILE = File.expand_path('../../config/resque.yml', __dir__)
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
class << self
2016-11-03 12:29:30 +05:30
# Do NOT cache in an instance variable. Result may be mutated by caller.
2016-09-13 17:45:13 +05:30
def params
2016-11-03 12:29:30 +05:30
new.params
2016-09-13 17:45:13 +05:30
end
2016-11-03 12:29:30 +05:30
# Do NOT cache in an instance variable. Result may be mutated by caller.
2016-09-13 17:45:13 +05:30
# @deprecated Use .params instead to get sentinel support
def url
new.url
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
def with
2016-11-03 12:29:30 +05:30
@pool ||= ConnectionPool.new(size: pool_size) { ::Redis.new(params) }
2016-09-13 17:45:13 +05:30
@pool.with { |redis| yield redis }
end
2016-11-03 12:29:30 +05:30
def pool_size
if Sidekiq.server?
# the pool will be used in a multi-threaded context
Sidekiq.options[:concurrency] + 5
else
# probably this is a Unicorn process, so single threaded
5
end
end
def _raw_config
return @_raw_config if defined?(@_raw_config)
begin
@_raw_config = File.read(CONFIG_FILE).freeze
rescue Errno::ENOENT
@_raw_config = false
end
@_raw_config
2016-06-02 11:05:42 +05:30
end
end
2016-09-13 17:45:13 +05:30
def initialize(rails_env = nil)
@rails_env = rails_env || ::Rails.env
end
def params
redis_store_options
end
def url
raw_config_hash[:url]
end
private
def redis_store_options
config = raw_config_hash
redis_url = config.delete(:url)
redis_uri = URI.parse(redis_url)
2016-06-02 11:05:42 +05:30
if redis_uri.scheme == 'unix'
2016-09-13 17:45:13 +05:30
# Redis::Store does not handle Unix sockets well, so let's do it for them
config[:path] = redis_uri.path
config
else
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-06-02 11:05:42 +05:30
end
end
2016-09-13 17:45:13 +05:30
def raw_config_hash
config_data = fetch_config
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
if config_data
config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
else
{ url: DEFAULT_REDIS_URL }
2016-06-02 11:05:42 +05:30
end
end
2016-09-13 17:45:13 +05:30
def fetch_config
2016-11-03 12:29:30 +05:30
self.class._raw_config ? YAML.load(self.class._raw_config)[@rails_env] : false
2016-09-13 17:45:13 +05:30
end
2016-06-02 11:05:42 +05:30
end
end