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.


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

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

Class Method Details

._raw_configObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/redis/wrapper.rb', line 39

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


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/redis/wrapper.rb', line 66

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.


62
63
64
# File 'lib/gitlab/redis/wrapper.rb', line 62

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

.default_urlObject


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

def default_url
  DEFAULT_REDIS_URL
end

.instrumentation_classObject

Raises:

  • (NotImplementedError)

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

def instrumentation_class
  raise NotImplementedError
end

.poolObject


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

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

.pool_sizeObject


29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/redis/wrapper.rb', line 29

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

.versionObject


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

def version
  with { |redis| redis.info['redis_version'] }
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


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

def params
  redis_store_options
end

#sentinelsObject


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

def sentinels
  raw_config_hash[:sentinels]
end

#sentinels?Boolean

Returns:


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

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

#urlObject


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

def url
  raw_config_hash[:url]
end