BigW Consortium Gitlab

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

5
module Gitlab
6
  class Redis
7
    CACHE_NAMESPACE = 'cache:gitlab'
8 9
    SESSION_NAMESPACE = 'session:gitlab'
    SIDEKIQ_NAMESPACE = 'resque:gitlab'
10 11
    MAILROOM_NAMESPACE = 'mail_room:gitlab'
    DEFAULT_REDIS_URL = 'redis://localhost:6379'
12
    CONFIG_FILE = File.expand_path('../../config/resque.yml', __dir__)
13

14
    class << self
15
      # Do NOT cache in an instance variable. Result may be mutated by caller.
16
      def params
17
        new.params
18 19
      end

20
      # Do NOT cache in an instance variable. Result may be mutated by caller.
21 22
      # @deprecated Use .params instead to get sentinel support
      def url
23
        new.url
24
      end
25

26
      def with
27
        @pool ||= ConnectionPool.new(size: pool_size) { ::Redis.new(params) }
28
        @pool.with { |redis| yield redis }
29
      end
30

31 32 33 34 35 36 37 38 39 40
      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

41 42 43
      def _raw_config
        return @_raw_config if defined?(@_raw_config)

44
        begin
45
          @_raw_config = ERB.new(File.read(CONFIG_FILE)).result.freeze
46 47
        rescue Errno::ENOENT
          @_raw_config = false
48
        end
49

50
        @_raw_config
51
      end
52
    end
53

54
    def initialize(rails_env = nil)
55
      @rails_env = rails_env || ::Rails.env
56 57 58 59 60 61
    end

    def params
      redis_store_options
    end

62 63 64 65
    def url
      raw_config_hash[:url]
    end

66 67 68 69 70 71 72 73
    def sentinels
      raw_config_hash[:sentinels]
    end

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

74 75 76 77
    private

    def redis_store_options
      config = raw_config_hash
78 79
      redis_url = config.delete(:url)
      redis_uri = URI.parse(redis_url)
80

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

93 94
    def raw_config_hash
      config_data = fetch_config
95

96 97 98 99
      if config_data
        config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
      else
        { url: DEFAULT_REDIS_URL }
100 101
      end
    end
102 103

    def fetch_config
104
      self.class._raw_config ? YAML.load(self.class._raw_config)[@rails_env] : false
105
    end
106
  end
107
end