Skip to content

Commit

Permalink
THREESCALE-11062: Redis config: extract DB from URL (#3800)
Browse files Browse the repository at this point in the history
* Redis config: extract DB from URL

* Add a unit test

* Remove `#db`
  • Loading branch information
jlledom authored May 22, 2024
1 parent c074e47 commit d65dbd2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
9 changes: 1 addition & 8 deletions app/lib/three_scale/redis_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ def initialize(redis_config = {})
sentinels = raw_config.delete(:sentinels).presence
raw_config.delete_if { |key, value| value.blank? }
raw_config[:size] ||= raw_config.delete(:pool_size) if raw_config.key?(:pool_size)
raw_config[:db] ||= URI.parse(raw_config[:url].to_s).path[1..]

@config = ActiveSupport::OrderedOptions.new.merge(raw_config)
config.sentinels = parse_sentinels(sentinels) if sentinels
end

attr_reader :config

def db
value = config.db.presence
return value.to_i if value
url = config.url.presence
return unless url
URI.parse(url).path[1..-1].to_s.to_i
end

def reverse_merge(other)
other.merge(config)
end
Expand Down
14 changes: 8 additions & 6 deletions test/unit/three_scale/redis_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

module ThreeScale
class RedisConfigTest < ActiveSupport::TestCase
test '#db' do
assert_equal 4, RedisConfig.new(db: 4).db
assert_equal 3, RedisConfig.new(url: 'redis://my-redis/3').db
assert_equal 0, RedisConfig.new(url: 'redis://my-redis').db
end

test '#reverse_merge' do
config_1 = RedisConfig.new(host: 'localhost', db: 1)
config_2 = RedisConfig.new(db: 2, password: 'passwd')
Expand Down Expand Up @@ -37,6 +31,14 @@ class RedisConfigTest < ActiveSupport::TestCase
assert_equal expected_sentinels, config.sentinels
end

test 'extracts the Redis logical DB from the URL' do
config = RedisConfig.new(url: 'redis://localhost:6379/6')

assert config.key? :db
assert_equal '6', config[:db]
assert_equal '6', config.db
end

test ':pool_size is renamed to :size' do
config = RedisConfig.new(pool_size: 5)

Expand Down

0 comments on commit d65dbd2

Please sign in to comment.