Browse Source

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
closed-social-glitch-2
beatrix 7 years ago
committed by Eugen Rochko
parent
commit
5c1f70b5c5
5 changed files with 39 additions and 14 deletions
  1. +1
    -0
      Gemfile
  2. +4
    -1
      Gemfile.lock
  3. +12
    -5
      config/initializers/redis.rb
  4. +9
    -2
      config/initializers/sidekiq.rb
  5. +13
    -6
      streaming/index.js

+ 1
- 0
Gemfile 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'

+ 4
- 1
Gemfile.lock 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

+ 12
- 5
config/initializers/redis.rb 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
)
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

+ 9
- 2
config/initializers/sidekiq.rb 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

+ 13
- 6
streaming/index.js 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

Loading…
Cancel
Save