Class: Gitlab::Redis::Wrapper

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

Constant Summary collapse

InvalidPathError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_env = nil) ⇒ Wrapper

Returns a new instance of Wrapper.



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

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

Class Method Details

.config_fallbackObject



76
77
78
# File 'lib/gitlab/redis/wrapper.rb', line 76

def config_fallback
  nil
end

.config_file_nameObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/gitlab/redis/wrapper.rb', line 57

def config_file_name
  [
    # Instance specific config sources:
    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).
    config_fallback&.config_file_name
  ].compact.first
end

.config_file_path(filename) ⇒ Object



46
47
48
49
# File 'lib/gitlab/redis/wrapper.rb', line 46

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

.instrumentation_classObject



80
81
82
83
84
# File 'lib/gitlab/redis/wrapper.rb', line 80

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

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

.poolObject



32
33
34
# File 'lib/gitlab/redis/wrapper.rb', line 32

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

.pool_sizeObject



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

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.



53
54
55
# File 'lib/gitlab/redis/wrapper.rb', line 53

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

.redis_yml_pathObject



68
69
70
# File 'lib/gitlab/redis/wrapper.rb', line 68

def redis_yml_path
  File.join(rails_root, 'config/redis.yml')
end

.store_nameObject



72
73
74
# File 'lib/gitlab/redis/wrapper.rb', line 72

def store_name
  name.demodulize
end

.versionObject



28
29
30
# File 'lib/gitlab/redis/wrapper.rb', line 28

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

.withObject



24
25
26
# File 'lib/gitlab/redis/wrapper.rb', line 24

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

Instance Method Details

#dbObject



105
106
107
# File 'lib/gitlab/redis/wrapper.rb', line 105

def db
  redis_store_options[:db]
end

#paramsObject



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

def params
  redis_store_options
end

#sentinelsObject



109
110
111
# File 'lib/gitlab/redis/wrapper.rb', line 109

def sentinels
  raw_config_hash[:sentinels]
end

#sentinels?Boolean

Returns:



113
114
115
# File 'lib/gitlab/redis/wrapper.rb', line 113

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

#store(extras = {}) ⇒ Object



117
118
119
# File 'lib/gitlab/redis/wrapper.rb', line 117

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

#urlObject



101
102
103
# File 'lib/gitlab/redis/wrapper.rb', line 101

def url
  raw_config_hash[:url]
end