Commit 0163e276 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add Gitlab::Redis connection pool

parent ba7fcc98
...@@ -4,7 +4,7 @@ require 'rails/all' ...@@ -4,7 +4,7 @@ require 'rails/all'
require 'devise' require 'devise'
I18n.config.enforce_available_locales = false I18n.config.enforce_available_locales = false
Bundler.require(:default, Rails.env) Bundler.require(:default, Rails.env)
require_relative '../lib/gitlab/redis_config' require_relative '../lib/gitlab/redis'
module Gitlab module Gitlab
REDIS_CACHE_NAMESPACE = 'cache:gitlab' REDIS_CACHE_NAMESPACE = 'cache:gitlab'
...@@ -69,7 +69,7 @@ module Gitlab ...@@ -69,7 +69,7 @@ module Gitlab
end end
end end
redis_config_hash = Gitlab::RedisConfig.redis_store_options redis_config_hash = Gitlab::Redis.redis_store_options
redis_config_hash[:namespace] = REDIS_CACHE_NAMESPACE redis_config_hash[:namespace] = REDIS_CACHE_NAMESPACE
redis_config_hash[:expires_in] = 2.weeks # Cache should not grow forever redis_config_hash[:expires_in] = 2.weeks # Cache should not grow forever
config.cache_store = :redis_store, redis_config_hash config.cache_store = :redis_store, redis_config_hash
......
...@@ -13,7 +13,7 @@ end ...@@ -13,7 +13,7 @@ end
if Rails.env.test? if Rails.env.test?
Gitlab::Application.config.session_store :cookie_store, key: "_gitlab_session" Gitlab::Application.config.session_store :cookie_store, key: "_gitlab_session"
else else
redis_config = Gitlab::RedisConfig.redis_store_options redis_config = Gitlab::Redis.redis_store_options
redis_config[:namespace] = 'session:gitlab' redis_config[:namespace] = 'session:gitlab'
Gitlab::Application.config.session_store( Gitlab::Application.config.session_store(
......
...@@ -2,7 +2,7 @@ SIDEKIQ_REDIS_NAMESPACE = 'resque:gitlab' ...@@ -2,7 +2,7 @@ SIDEKIQ_REDIS_NAMESPACE = 'resque:gitlab'
Sidekiq.configure_server do |config| Sidekiq.configure_server do |config|
config.redis = { config.redis = {
url: Gitlab::RedisConfig.url, url: Gitlab::Redis.url,
namespace: SIDEKIQ_REDIS_NAMESPACE namespace: SIDEKIQ_REDIS_NAMESPACE
} }
...@@ -29,7 +29,7 @@ end ...@@ -29,7 +29,7 @@ end
Sidekiq.configure_client do |config| Sidekiq.configure_client do |config|
config.redis = { config.redis = {
url: Gitlab::RedisConfig.url, url: Gitlab::Redis.url,
namespace: SIDEKIQ_REDIS_NAMESPACE namespace: SIDEKIQ_REDIS_NAMESPACE
} }
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<% <%
require "yaml" require "yaml"
require "json" require "json"
require_relative "lib/gitlab/redis_config" require_relative "lib/gitlab/redis"
rails_env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" rails_env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
...@@ -18,7 +18,7 @@ if File.exists?(config_file) ...@@ -18,7 +18,7 @@ if File.exists?(config_file)
config['mailbox'] = "inbox" if config['mailbox'].nil? config['mailbox'] = "inbox" if config['mailbox'].nil?
if config['enabled'] && config['address'] if config['enabled'] && config['address']
redis_url = Gitlab::RedisConfig.new(rails_env).url redis_url = Gitlab::Redis.new(rails_env).url
%> %>
- -
:host: <%= config['host'].to_json %> :host: <%= config['host'].to_json %>
......
...@@ -43,8 +43,10 @@ module Gitlab ...@@ -43,8 +43,10 @@ module Gitlab
# false if the lease is already taken. # false if the lease is already taken.
def try_obtain def try_obtain
# Performing a single SET is atomic # Performing a single SET is atomic
Gitlab::Redis.with do |redis|
!!redis.set(redis_key, '1', nx: true, ex: @timeout) !!redis.set(redis_key, '1', nx: true, ex: @timeout)
end end
end
# No #cancel method. See comments above! # No #cancel method. See comments above!
......
module Gitlab module Gitlab
class RedisConfig class Redis
attr_reader :url attr_reader :url
def self.url def self.url
new.url @url ||= new.url
end
def self.with
@pool ||= ConnectionPool.new { ::Redis.new(url: url) }
@pool.with { |redis| yield redis }
end end
def self.redis_store_options def self.redis_store_options
url = new.url url = new.url
redis_config_hash = Redis::Store::Factory.extract_host_options_from_uri(url) redis_config_hash = ::Redis::Store::Factory.extract_host_options_from_uri(url)
# Redis::Store does not handle Unix sockets well, so let's do it for them # Redis::Store does not handle Unix sockets well, so let's do it for them
redis_uri = URI.parse(url) redis_uri = URI.parse(url)
if redis_uri.scheme == 'unix' if redis_uri.scheme == 'unix'
......
...@@ -4,7 +4,7 @@ namespace :cache do ...@@ -4,7 +4,7 @@ namespace :cache do
desc "GitLab | Clear redis cache" desc "GitLab | Clear redis cache"
task :clear => :environment do task :clear => :environment do
redis = Redis.new(url: Gitlab::RedisConfig.url) Gitlab::Redis.with do |redis|
cursor = REDIS_SCAN_START_STOP cursor = REDIS_SCAN_START_STOP
loop do loop do
cursor, keys = redis.scan( cursor, keys = redis.scan(
...@@ -18,4 +18,5 @@ namespace :cache do ...@@ -18,4 +18,5 @@ namespace :cache do
break if cursor == REDIS_SCAN_START_STOP break if cursor == REDIS_SCAN_START_STOP
end end
end end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment