Support REDIS_SENTINEL_PORT variables (#31767)

This commit is contained in:
David Roetzel 2024-09-05 11:26:49 +02:00 committed by GitHub
parent 4d5c91e99a
commit 7d91723f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 15 deletions

View File

@ -64,10 +64,13 @@ class Mastodon::RedisConfiguration
port = ENV.fetch("#{prefix}PORT", defaults[:port])
db = ENV.fetch("#{prefix}DB", defaults[:db])
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
sentinels = parse_sentinels(ENV.fetch("#{prefix}SENTINELS", nil))
sentinel_port = ENV.fetch("#{prefix}SENTINEL_PORT", 26_379)
sentinel_list = ENV.fetch("#{prefix}SENTINELS", nil)
return { url:, driver: } if url
sentinels = parse_sentinels(sentinel_list, default_port: sentinel_port)
if name.present? && sentinels.present?
host = name
port = nil
@ -96,10 +99,10 @@ class Mastodon::RedisConfiguration
end.normalize.to_str
end
def parse_sentinels(sentinels_string)
def parse_sentinels(sentinels_string, default_port: 26_379)
(sentinels_string || '').split(',').map do |sentinel|
host, port = sentinel.split(':')
port = port.present? ? port.to_i : 26_379
port = (port || default_port).to_i
{ host: host, port: port }
end.presence
end

View File

@ -107,6 +107,7 @@ RSpec.describe Mastodon::RedisConfiguration do
end
context 'when giving sentinels without port numbers' do
context "when no default port is given via `#{prefix}REDIS_SENTINEL_PORT`" do
around do |example|
ClimateControl.modify "#{prefix}REDIS_SENTINELS": '192.168.0.1,192.168.0.2', "#{prefix}REDIS_SENTINEL_MASTER": 'mainsentinel' do
example.run
@ -117,6 +118,31 @@ RSpec.describe Mastodon::RedisConfiguration do
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 26_379 }, { host: '192.168.0.2', port: 26_379 })
end
end
context 'when adding port numbers to some, but not all sentinels' do
around do |example|
ClimateControl.modify "#{prefix}REDIS_SENTINELS": '192.168.0.1:5678,192.168.0.2', "#{prefix}REDIS_SENTINEL_MASTER": 'mainsentinel' do
example.run
end
end
it 'uses the given port number when available and the default otherwise' do
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 5678 }, { host: '192.168.0.2', port: 26_379 })
end
end
context "when a default port is given via `#{prefix}REDIS_SENTINEL_PORT`" do
around do |example|
ClimateControl.modify "#{prefix}REDIS_SENTINEL_PORT": '1234', "#{prefix}REDIS_SENTINELS": '192.168.0.1,192.168.0.2', "#{prefix}REDIS_SENTINEL_MASTER": 'mainsentinel' do
example.run
end
end
it 'uses the given port number' do
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 1234 }, { host: '192.168.0.2', port: 1234 })
end
end
end
end
describe '#base' do