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

103 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'
2017-08-17 22:00:37 +05:30
require 'active_support/core_ext/module/delegation'
2016-09-13 17:45:13 +05:30
2016-06-02 11:05:42 +05:30
module Gitlab
class Redis
2017-08-17 22:00:37 +05:30
CACHE_NAMESPACE = 'cache:gitlab'.freeze
SESSION_NAMESPACE = 'session:gitlab'.freeze
SIDEKIQ_NAMESPACE = 'resque:gitlab'.freeze
MAILROOM_NAMESPACE = 'mail_room:gitlab'.freeze
DEFAULT_REDIS_URL = 'redis://localhost:6379'.freeze
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
class << self
2017-08-17 22:00:37 +05:30
delegate :params, :url, to: :new
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
2017-08-17 22:00:37 +05:30
@_raw_config = ERB.new(File.read(config_file)).result.freeze
2016-11-03 12:29:30 +05:30
rescue Errno::ENOENT
@_raw_config = false
end
@_raw_config
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def config_file
ENV['GITLAB_REDIS_CONFIG_FILE'] || File.expand_path('../../config/resque.yml', __dir__)
end
2016-06-02 11:05:42 +05:30
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
2017-08-17 22:00:37 +05:30
def sentinels
raw_config_hash[:sentinels]
end
def sentinels?
sentinels && !sentinels.empty?
end
2016-09-13 17:45:13 +05:30
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