From 5c1f70b5c577123cc42e073dea6a8f72e27818f8 Mon Sep 17 00:00:00 2001 From: beatrix Date: Sun, 7 May 2017 13:42:32 -0400 Subject: [PATCH] namespace redis usage (#2869) * add redis-namespace gem * namespace redis usage * refactor redis namespace code to be less intrusive previously : would be prepended to keys when the REDIS_NAMESPACE env var was not set now if it is not set the namespacing functions are not used at all, which should prevent disruptions when instances update. * fix redis namespace variable style in streaming js * remove trailing space * final redis namespace style fix --- Gemfile | 1 + Gemfile.lock | 5 ++++- config/initializers/redis.rb | 17 ++++++++++++----- config/initializers/sidekiq.rb | 11 +++++++++-- streaming/index.js | 19 +++++++++++++------ 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 7658d64763..ff8e8a3d18 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem 'doorkeeper' gem 'fast_blank' gem 'goldfinger' gem 'hiredis' +gem 'redis-namespace' gem 'htmlentities' gem 'http' gem 'http_accept_language' diff --git a/Gemfile.lock b/Gemfile.lock index 75b854b706..99f07bc125 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -340,9 +340,11 @@ GEM redis-activesupport (5.0.2) activesupport (>= 3, < 6) redis-store (~> 1.3.0) + redis-namespace (1.5.3) + redis (~> 3.0, >= 3.0.4) + redis-store (>= 1.2, < 1.4) redis-rack (2.0.2) rack (>= 1.5, < 3) - redis-store (>= 1.2, < 1.4) redis-rails (5.0.2) redis-actionpack (>= 5.0, < 6) redis-activesupport (>= 5.0, < 6) @@ -524,6 +526,7 @@ DEPENDENCIES rails-settings-cached rails_12factor redis (~> 3.2) + redis-namespace redis-rails rqrcode rspec-rails diff --git a/config/initializers/redis.rb b/config/initializers/redis.rb index dd7fa112ed..63fc425573 100644 --- a/config/initializers/redis.rb +++ b/config/initializers/redis.rb @@ -9,14 +9,21 @@ if ENV['REDIS_URL'].blank? ENV['REDIS_URL'] = "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}" end -Redis.current = Redis.new( +redis_connection = Redis.new( url: ENV['REDIS_URL'], driver: :hiredis ) +cache_params = { expires_in: 10.minutes } + +namespace = ENV.fetch('REDIS_NAMESPACE') { nil } +if namespace + Redis.current = Redis::Namespace.new(namespace, :redis => redis_connection) + cache_params[:namespace] = namespace + '_cache' +else + Redis.current = redis_connection +end + Rails.application.configure do - config.cache_store = :redis_store, ENV['REDIS_URL'], { - namespace: 'cache', - expires_in: 10.minutes, - } + config.cache_store = :redis_store, ENV['REDIS_URL'], cache_params end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 3d2e4741df..b70784d79a 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,9 +1,16 @@ # frozen_string_literal: true +namespace = ENV.fetch('REDIS_NAMESPACE') { nil } +redis_params = { url: ENV['REDIS_URL'] } + +if namespace + redis_params [:namespace] = namespace +end + Sidekiq.configure_server do |config| - config.redis = { url: ENV['REDIS_URL'] } + config.redis = redis_params end Sidekiq.configure_client do |config| - config.redis = { url: ENV['REDIS_URL'] } + config.redis = redis_params end diff --git a/streaming/index.js b/streaming/index.js index 3a7764ee98..d3fb8d6470 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -87,13 +87,21 @@ if (cluster.isMaster) { const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL))) const server = http.createServer(app) const wss = new WebSocket.Server({ server }) + const redisNamespace = process.env.REDIS_NAMESPACE || null - const redisClient = redis.createClient({ + const redisParams = { host: process.env.REDIS_HOST || '127.0.0.1', port: process.env.REDIS_PORT || 6379, password: process.env.REDIS_PASSWORD, url: process.env.REDIS_URL || null - }) + } + + if (redisNamespace) { + redisParams.namespace = redisNamespace + } + const redisPrefix = redisNamespace ? `${redisNamespace}:` : '' + + const redisClient = redis.createClient(redisParams) const subs = {} @@ -105,11 +113,10 @@ if (cluster.isMaster) { if (!callbacks) { return } - callbacks.forEach(callback => callback(message)) }) - redisClient.psubscribe('timeline:*') + redisClient.psubscribe(`${redisPrefix}timeline:*`) const subscribe = (channel, callback) => { log.silly(`Adding listener for ${channel}`) @@ -242,8 +249,8 @@ if (cluster.isMaster) { } } - subscribe(id, listener) - attachCloseHandler(id, listener) + subscribe(`${redisPrefix}${id}`, listener) + attachCloseHandler(`${redisPrefix}${id}`, listener) } // Setup stream output to HTTP