Class: Gitlab::Redis::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/redis/wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_env = nil) ⇒ Wrapper

Returns a new instance of Wrapper.


115
116
117
# File 'lib/gitlab/redis/wrapper.rb', line 115

def initialize(rails_env = nil)
  @rails_env = rails_env || ::Rails.env
end

Class Method Details

._raw_configObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitlab/redis/wrapper.rb', line 44

def _raw_config
  return @_raw_config if defined?(@_raw_config)

  @_raw_config =
    begin
      if filename = config_file_name
        ERB.new(File.read(filename)).result.freeze
      else
        false
      end
    rescue Errno::ENOENT
      false
    end
end

.config_fallbackObject


98
99
100
# File 'lib/gitlab/redis/wrapper.rb', line 98

def config_fallback
  nil
end

.config_fallback?Boolean

Returns:


70
71
72
# File 'lib/gitlab/redis/wrapper.rb', line 70

def config_fallback?
  config_file_name == config_fallback&.config_file_name
end

.config_file_nameObject


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gitlab/redis/wrapper.rb', line 74

def config_file_name
  [
    # Instance specific config sources:
    ENV["GITLAB_REDIS_#{store_name.underscore.upcase}_CONFIG_FILE"],
    config_file_path("redis.#{store_name.underscore}.yml"),

    # The current Redis instance may have been split off from another one
    # (e.g. TraceChunks was split off from SharedState). There are
    # installations out there where the lowest priority config source
    # (resque.yml) contains bogus values. In those cases, config_file_name
    # should resolve to the instance we originated from (the
    # "config_fallback") rather than resque.yml.
    config_fallback&.config_file_name,

    # Global config sources:
    ENV['GITLAB_REDIS_CONFIG_FILE'],
    config_file_path('resque.yml')
  ].compact.first
end

.config_file_path(filename) ⇒ Object


59
60
61
62
# File 'lib/gitlab/redis/wrapper.rb', line 59

def config_file_path(filename)
  path = File.join(rails_root, 'config', filename)
  return path if File.file?(path)
end

.instrumentation_classObject


102
103
104
105
106
# File 'lib/gitlab/redis/wrapper.rb', line 102

def instrumentation_class
  return unless defined?(::Gitlab::Instrumentation::Redis)

  "::Gitlab::Instrumentation::Redis::#{store_name}".constantize
end

.poolObject


30
31
32
# File 'lib/gitlab/redis/wrapper.rb', line 30

def pool
  @pool ||= ConnectionPool.new(size: pool_size) { redis }
end

.pool_sizeObject


34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/redis/wrapper.rb', line 34

def pool_size
  # heuristic constant 5 should be a config setting somewhere -- related to CPU count?
  size = 5
  if Gitlab::Runtime.multi_threaded?
    size += Gitlab::Runtime.max_threads
  end

  size
end

.rails_rootObject

We need this local implementation of Rails.root because MailRoom doesn't load Rails.


66
67
68
# File 'lib/gitlab/redis/wrapper.rb', line 66

def rails_root
  File.expand_path('../../..', __dir__)
end

.store_nameObject


94
95
96
# File 'lib/gitlab/redis/wrapper.rb', line 94

def store_name
  name.demodulize
end

.versionObject


26
27
28
# File 'lib/gitlab/redis/wrapper.rb', line 26

def version
  with { |redis| redis.info['redis_version'] }
end

.withObject


22
23
24
# File 'lib/gitlab/redis/wrapper.rb', line 22

def with
  pool.with { |redis| yield redis }
end

Instance Method Details

#dbObject


127
128
129
# File 'lib/gitlab/redis/wrapper.rb', line 127

def db
  redis_store_options[:db]
end

#paramsObject


119
120
121
# File 'lib/gitlab/redis/wrapper.rb', line 119

def params
  redis_store_options
end

#sentinelsObject


131
132
133
# File 'lib/gitlab/redis/wrapper.rb', line 131

def sentinels
  raw_config_hash[:sentinels]
end

#sentinels?Boolean

Returns:


135
136
137
# File 'lib/gitlab/redis/wrapper.rb', line 135

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

#store(extras = {}) ⇒ Object


139
140
141
# File 'lib/gitlab/redis/wrapper.rb', line 139

def store(extras = {})
  ::Redis::Store::Factory.create(redis_store_options.merge(extras))
end

#urlObject


123
124
125
# File 'lib/gitlab/redis/wrapper.rb', line 123

def url
  raw_config_hash[:url]
end