Class: Gitlab::Redis::Wrapper

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

Direct Known Subclasses

Cache, Queues, SharedState

Constant Summary collapse

DEFAULT_REDIS_URL =
'redis://localhost:6379'
REDIS_CONFIG_ENV_VAR_NAME =
'GITLAB_REDIS_CONFIG_FILE'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_env = nil) ⇒ Wrapper

Returns a new instance of Wrapper.


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

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

Class Method Details

._raw_configObject


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

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_file_nameObject


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/redis/wrapper.rb', line 62

def config_file_name
  # if ENV set for wrapper class, use it even if it points to a file does not exist
  file_name = ENV[REDIS_CONFIG_ENV_VAR_NAME]
  return file_name unless file_name.nil?

  # otherwise, if config files exists for wrapper class, use it
  file_name = config_file_path('resque.yml')
  return file_name if File.file?(file_name)

  # nil will force use of DEFAULT_REDIS_URL when config file is absent
  nil
end

.config_file_path(filename) ⇒ Object

Return the absolute path to a Rails configuration file

We use this instead of `Rails.root` because for certain tasks utilizing these classes, `Rails` might not be available.


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

def config_file_path(filename)
  File.expand_path("../../../config/#{filename}", __dir__)
end

.default_urlObject


50
51
52
# File 'lib/gitlab/redis/wrapper.rb', line 50

def default_url
  DEFAULT_REDIS_URL
end

.instrumentation_classObject

Raises:

  • (NotImplementedError)

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

def instrumentation_class
  raise NotImplementedError
end

.poolObject


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

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

.pool_sizeObject


25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/redis/wrapper.rb', line 25

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

.withObject


17
18
19
# File 'lib/gitlab/redis/wrapper.rb', line 17

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

Instance Method Details

#paramsObject


84
85
86
# File 'lib/gitlab/redis/wrapper.rb', line 84

def params
  redis_store_options
end

#sentinelsObject


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

def sentinels
  raw_config_hash[:sentinels]
end

#sentinels?Boolean

Returns:


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

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

#urlObject


88
89
90
# File 'lib/gitlab/redis/wrapper.rb', line 88

def url
  raw_config_hash[:url]
end