Skip to content

Commit

Permalink
chore: refactored redis config (sentinel related) (#1327)
Browse files Browse the repository at this point in the history
Refactored the redis config module which had redis sentinel related
configs. Also changed the specs accordingly.
  • Loading branch information
sony-mathew authored Oct 9, 2020
1 parent b1a8430 commit e01fdb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
55 changes: 26 additions & 29 deletions lib/redis/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,42 @@ module Redis::Config

class << self
def app
return BASE_CONFIG if const_defined? 'BASE_CONFIG'

reload_config
config
end

def sidekiq
config = begin
if const_defined? 'BASE_CONFIG'
BASE_CONFIG
else
reload_config
end
end
config.merge(size: SIDEKIQ_SIZE)
app.merge(size: SIDEKIQ_SIZE)
end

def reload_config
config = {
def config
@config ||= sentinel? ? sentinel_config : base_config
end

def base_config
{
url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'),
password: ENV.fetch('REDIS_PASSWORD', nil).presence
}
end

sentinel_string = ENV.fetch('REDIS_SENTINELS', nil)
if sentinel_string.presence
# expected format for REDIS_SENTINELS url string is host1:port1, host2:port2
sentinels = sentinel_string.split(',').map do |sentinel_url|
host, port = sentinel_url.split(':').map(&:strip)
{ host: host, port: port || DEFAULT_SENTINEL_PORT, password: config[:password] }
end

master_name = ENV.fetch('REDIS_SENTINEL_MASTER_NAME', 'mymaster')
# over-write redis url as redis://:<your-redis-password>@<master-name>/ when using sentinel
# more at https://github.com/redis/redis-rb/issues/531#issuecomment-263501322
config[:url] = "redis://#{master_name}"
config[:sentinels] = sentinels
def sentinel?
ENV.fetch('REDIS_SENTINELS', nil).presence
end

def sentinel_config
redis_sentinels = ENV.fetch('REDIS_SENTINELS', nil)

# expected format for REDIS_SENTINELS url string is host1:port1, host2:port2
sentinels = redis_sentinels.split(',').map do |sentinel_url|
host, port = sentinel_url.split(':').map(&:strip)
{ host: host, port: port.presence || DEFAULT_SENTINEL_PORT, password: base_config[:password] }
end
send(:remove_const, 'BASE_CONFIG') if const_defined? 'BASE_CONFIG'
const_set('BASE_CONFIG', config)
BASE_CONFIG

# over-write redis url as redis://:<your-redis-password>@<master-name>/ when using sentinel
# more at https://github.com/redis/redis-rb/issues/531#issuecomment-263501322
master = "redis://#{ENV.fetch('REDIS_SENTINEL_MASTER_NAME', 'mymaster')}"

base_config.merge({ url: master, sentinels: sentinels })
end
end
end
6 changes: 4 additions & 2 deletions spec/lib/redis/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
let(:redis_pasword) { 'some-strong-password' }

before do
described_class.instance_variable_set(:@config, nil)
allow(ENV).to receive(:fetch).with('REDIS_URL', 'redis://127.0.0.1:6379').and_return(redis_url)
allow(ENV).to receive(:fetch).with('REDIS_PASSWORD', nil).and_return(redis_pasword)
allow(ENV).to receive(:fetch).with('REDIS_SENTINELS', nil).and_return('')
allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return('')
described_class.reload_config
described_class.config
end

it 'checks for app redis config' do
Expand Down Expand Up @@ -44,11 +45,12 @@
end

before do
described_class.instance_variable_set(:@config, nil)
allow(ENV).to receive(:fetch).with('REDIS_URL', 'redis://127.0.0.1:6379').and_return(redis_url)
allow(ENV).to receive(:fetch).with('REDIS_PASSWORD', nil).and_return(redis_pasword)
allow(ENV).to receive(:fetch).with('REDIS_SENTINELS', nil).and_return(redis_sentinels)
allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return(redis_master_name)
described_class.reload_config
described_class.config
end

it 'checks for app redis config' do
Expand Down

0 comments on commit e01fdb5

Please sign in to comment.