diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2016-09-22 12:32:49 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2016-09-22 16:58:40 +0300 |
commit | b228b86b3e644c7c277b8004e3fd9291ba493cc2 (patch) | |
tree | 6b9c3578a70c2191f31ae2a4ad861153b57f53aa /lib/gitlab | |
parent | 98b3d6ce695a9751f72adc35bc09f82eb2f624a4 (diff) |
Make Gitlab::Redis.params safe for mutation
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/redis.rb | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/gitlab/redis.rb b/lib/gitlab/redis.rb index 9376b54f43b..69c4ef721d5 100644 --- a/lib/gitlab/redis.rb +++ b/lib/gitlab/redis.rb @@ -9,19 +9,22 @@ module Gitlab SIDEKIQ_NAMESPACE = 'resque:gitlab' MAILROOM_NAMESPACE = 'mail_room:gitlab' DEFAULT_REDIS_URL = 'redis://localhost:6379' + CONFIG_FILE = File.expand_path('../../config/resque.yml', __dir__) # To be thread-safe we must be careful when writing the class instance - # variables @url and @pool. Because @pool depends on @url we need two + # variables @_raw_config and @pool. Because @pool depends on @_raw_config we need two # mutexes to prevent deadlock. - PARAMS_MUTEX = Mutex.new + RAW_CONFIG_MUTEX = Mutex.new POOL_MUTEX = Mutex.new - private_constant :PARAMS_MUTEX, :POOL_MUTEX + private_constant :RAW_CONFIG_MUTEX, :POOL_MUTEX class << self + # Do NOT cache in an instance variable. Result may be mutated by caller. def params - @params || PARAMS_MUTEX.synchronize { @params = new.params } + new.params end + # Do NOT cache in an instance variable. Result may be mutated by caller. # @deprecated Use .params instead to get sentinel support def url new.url @@ -36,8 +39,17 @@ module Gitlab @pool.with { |redis| yield redis } end - def reset_params! - @params = nil + def _raw_config + return @_raw_config if defined?(@_raw_config) + + RAW_CONFIG_MUTEX.synchronize do + begin + @_raw_config = File.read(CONFIG_FILE).freeze + rescue Errno::ENOENT + @_raw_config = false + end + end + @_raw_config end end @@ -83,12 +95,7 @@ module Gitlab end def fetch_config - file = config_file - File.exist?(file) ? YAML.load_file(file)[@rails_env] : false - end - - def config_file - File.expand_path('../../../config/resque.yml', __FILE__) + self.class._raw_config ? YAML.load(self.class._raw_config)[@rails_env] : false end end end |