BigW Consortium Gitlab

redis.rb 2.57 KB
Newer Older
1 2 3
# This file should not have any direct dependency on Rails environment
# please require all dependencies below:
require 'active_support/core_ext/hash/keys'
4
require 'active_support/core_ext/module/delegation'
5

6
module Gitlab
7
  class Redis
8 9 10 11 12
    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
13

14
    class << self
Douwe Maan committed
15
      delegate :params, :url, to: :new
16

17
      def with
18
        @pool ||= ConnectionPool.new(size: pool_size) { ::Redis.new(params) }
19
        @pool.with { |redis| yield redis }
20
      end
21

22 23 24 25 26 27 28 29 30 31
      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

32 33 34
      def _raw_config
        return @_raw_config if defined?(@_raw_config)

35
        begin
36
          @_raw_config = ERB.new(File.read(config_file)).result.freeze
37 38
        rescue Errno::ENOENT
          @_raw_config = false
39
        end
40

41
        @_raw_config
42
      end
43 44 45 46

      def config_file
        ENV['GITLAB_REDIS_CONFIG_FILE'] || File.expand_path('../../config/resque.yml', __dir__)
      end
47
    end
48

49
    def initialize(rails_env = nil)
50
      @rails_env = rails_env || ::Rails.env
51 52 53 54 55 56
    end

    def params
      redis_store_options
    end

57 58 59 60
    def url
      raw_config_hash[:url]
    end

61 62 63 64 65 66 67 68
    def sentinels
      raw_config_hash[:sentinels]
    end

    def sentinels?
      sentinels && !sentinels.empty?
    end

69 70 71 72
    private

    def redis_store_options
      config = raw_config_hash
73 74
      redis_url = config.delete(:url)
      redis_uri = URI.parse(redis_url)
75

76
      if redis_uri.scheme == 'unix'
77 78
        # Redis::Store does not handle Unix sockets well, so let's do it for them
        config[:path] = redis_uri.path
79
        config
80
      else
81 82 83 84
        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)
85 86 87
      end
    end

88 89
    def raw_config_hash
      config_data = fetch_config
90

91 92 93 94
      if config_data
        config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
      else
        { url: DEFAULT_REDIS_URL }
95 96
      end
    end
97 98

    def fetch_config
99
      self.class._raw_config ? YAML.load(self.class._raw_config)[@rails_env] : false
100
    end
101
  end
102
end