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
This commit is contained in:
beatrix 2017-05-07 13:42:32 -04:00 committed by Eugen Rochko
parent c7848f54ff
commit 5c1f70b5c5
5 changed files with 40 additions and 15 deletions

View file

@ -26,6 +26,7 @@ gem 'doorkeeper'
gem 'fast_blank'
gem 'goldfinger'
gem 'hiredis'
gem 'redis-namespace'
gem 'htmlentities'
gem 'http'
gem 'http_accept_language'

View file

@ -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

View file

@ -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
)
Rails.application.configure do
config.cache_store = :redis_store, ENV['REDIS_URL'], {
namespace: 'cache',
expires_in: 10.minutes,
}
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'], cache_params
end

View file

@ -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

View file

@ -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