gearheads
/
mastodon
Archived
2
0
Fork 0

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
gh/stable
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 'fast_blank'
gem 'goldfinger' gem 'goldfinger'
gem 'hiredis' gem 'hiredis'
gem 'redis-namespace'
gem 'htmlentities' gem 'htmlentities'
gem 'http' gem 'http'
gem 'http_accept_language' gem 'http_accept_language'

View File

@ -340,9 +340,11 @@ GEM
redis-activesupport (5.0.2) redis-activesupport (5.0.2)
activesupport (>= 3, < 6) activesupport (>= 3, < 6)
redis-store (~> 1.3.0) 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) redis-rack (2.0.2)
rack (>= 1.5, < 3) rack (>= 1.5, < 3)
redis-store (>= 1.2, < 1.4)
redis-rails (5.0.2) redis-rails (5.0.2)
redis-actionpack (>= 5.0, < 6) redis-actionpack (>= 5.0, < 6)
redis-activesupport (>= 5.0, < 6) redis-activesupport (>= 5.0, < 6)
@ -524,6 +526,7 @@ DEPENDENCIES
rails-settings-cached rails-settings-cached
rails_12factor rails_12factor
redis (~> 3.2) redis (~> 3.2)
redis-namespace
redis-rails redis-rails
rqrcode rqrcode
rspec-rails rspec-rails

View File

@ -9,14 +9,21 @@ if ENV['REDIS_URL'].blank?
ENV['REDIS_URL'] = "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}" ENV['REDIS_URL'] = "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}"
end end
Redis.current = Redis.new( redis_connection = Redis.new(
url: ENV['REDIS_URL'], url: ENV['REDIS_URL'],
driver: :hiredis driver: :hiredis
) )
Rails.application.configure do cache_params = { expires_in: 10.minutes }
config.cache_store = :redis_store, ENV['REDIS_URL'], {
namespace: 'cache', namespace = ENV.fetch('REDIS_NAMESPACE') { nil }
expires_in: 10.minutes, 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 end

View File

@ -1,9 +1,16 @@
# frozen_string_literal: true # 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| Sidekiq.configure_server do |config|
config.redis = { url: ENV['REDIS_URL'] } config.redis = redis_params
end end
Sidekiq.configure_client do |config| Sidekiq.configure_client do |config|
config.redis = { url: ENV['REDIS_URL'] } config.redis = redis_params
end 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 pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL)))
const server = http.createServer(app) const server = http.createServer(app)
const wss = new WebSocket.Server({ server }) 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', host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379, port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD, password: process.env.REDIS_PASSWORD,
url: process.env.REDIS_URL || null url: process.env.REDIS_URL || null
}) }
if (redisNamespace) {
redisParams.namespace = redisNamespace
}
const redisPrefix = redisNamespace ? `${redisNamespace}:` : ''
const redisClient = redis.createClient(redisParams)
const subs = {} const subs = {}
@ -105,11 +113,10 @@ if (cluster.isMaster) {
if (!callbacks) { if (!callbacks) {
return return
} }
callbacks.forEach(callback => callback(message)) callbacks.forEach(callback => callback(message))
}) })
redisClient.psubscribe('timeline:*') redisClient.psubscribe(`${redisPrefix}timeline:*`)
const subscribe = (channel, callback) => { const subscribe = (channel, callback) => {
log.silly(`Adding listener for ${channel}`) log.silly(`Adding listener for ${channel}`)
@ -242,8 +249,8 @@ if (cluster.isMaster) {
} }
} }
subscribe(id, listener) subscribe(`${redisPrefix}${id}`, listener)
attachCloseHandler(id, listener) attachCloseHandler(`${redisPrefix}${id}`, listener)
} }
// Setup stream output to HTTP // Setup stream output to HTTP