Skip to content

Commit

Permalink
namespace redis usage (#2869)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
beatrix-bitrot authored and Gargron committed May 7, 2017
1 parent c7848f5 commit 5c1f70b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem 'doorkeeper'
gem 'fast_blank'
gem 'goldfinger'
gem 'hiredis'
gem 'redis-namespace'
gem 'htmlentities'
gem 'http'
gem 'http_accept_language'
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -524,6 +526,7 @@ DEPENDENCIES
rails-settings-cached
rails_12factor
redis (~> 3.2)
redis-namespace
redis-rails
rqrcode
rspec-rails
Expand Down
17 changes: 12 additions & 5 deletions config/initializers/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
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
11 changes: 9 additions & 2 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 13 additions & 6 deletions streaming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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}`)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c1f70b

Please sign in to comment.