chore: refactored redis config (sentinel related) (#1327)

Refactored the redis config module which had redis sentinel related
configs. Also changed the specs accordingly.
This commit is contained in:
Sony Mathew
2020-10-09 12:37:42 +05:30
committed by GitHub
parent b1a8430258
commit e01fdb5f00
2 changed files with 29 additions and 30 deletions

View File

@@ -4,45 +4,42 @@ module Redis::Config
class << self class << self
def app def app
return BASE_CONFIG if const_defined? 'BASE_CONFIG' config
reload_config
end end
def sidekiq def sidekiq
config = begin app.merge(size: SIDEKIQ_SIZE)
if const_defined? 'BASE_CONFIG'
BASE_CONFIG
else
reload_config
end
end
config.merge(size: SIDEKIQ_SIZE)
end end
def reload_config def config
config = { @config ||= sentinel? ? sentinel_config : base_config
end
def base_config
{
url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'), url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'),
password: ENV.fetch('REDIS_PASSWORD', nil).presence password: ENV.fetch('REDIS_PASSWORD', nil).presence
} }
end
sentinel_string = ENV.fetch('REDIS_SENTINELS', nil) def sentinel?
if sentinel_string.presence ENV.fetch('REDIS_SENTINELS', nil).presence
# expected format for REDIS_SENTINELS url string is host1:port1, host2:port2 end
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') def sentinel_config
# over-write redis url as redis://:<your-redis-password>@<master-name>/ when using sentinel redis_sentinels = ENV.fetch('REDIS_SENTINELS', nil)
# more at https://github.com/redis/redis-rb/issues/531#issuecomment-263501322
config[:url] = "redis://#{master_name}" # expected format for REDIS_SENTINELS url string is host1:port1, host2:port2
config[:sentinels] = sentinels 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 end
send(:remove_const, 'BASE_CONFIG') if const_defined? 'BASE_CONFIG'
const_set('BASE_CONFIG', config) # over-write redis url as redis://:<your-redis-password>@<master-name>/ when using sentinel
BASE_CONFIG # 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 end
end end

View File

@@ -6,11 +6,12 @@ describe ::Redis::Config do
let(:redis_pasword) { 'some-strong-password' } let(:redis_pasword) { 'some-strong-password' }
before do 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_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_PASSWORD', nil).and_return(redis_pasword)
allow(ENV).to receive(:fetch).with('REDIS_SENTINELS', nil).and_return('') allow(ENV).to receive(:fetch).with('REDIS_SENTINELS', nil).and_return('')
allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return('') allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return('')
described_class.reload_config described_class.config
end end
it 'checks for app redis config' do it 'checks for app redis config' do
@@ -44,11 +45,12 @@ describe ::Redis::Config do
end end
before do 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_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_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_SENTINELS', nil).and_return(redis_sentinels)
allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return(redis_master_name) allow(ENV).to receive(:fetch).with('REDIS_SENTINEL_MASTER_NAME', 'mymaster').and_return(redis_master_name)
described_class.reload_config described_class.config
end end
it 'checks for app redis config' do it 'checks for app redis config' do