DEV: Readonly Redis support for `DiscourseRedis#multi/pipelined` (#16744)

Follow-up to 2df3c65ba9
This commit is contained in:
Alan Guo Xiang Tan 2022-05-13 16:18:13 +08:00 committed by GitHub
parent 2cc9f0e7d9
commit de9fe907ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 10 deletions

View File

@ -151,22 +151,26 @@ class DiscourseRedis
end
def multi
if block_given?
@redis.multi do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
DiscourseRedis.ignore_readonly do
if block_given?
@redis.multi do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
end
else
@redis.multi
end
else
@redis.multi
end
end
def pipelined
if block_given?
@redis.pipelined do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
DiscourseRedis.ignore_readonly do
if block_given?
@redis.pipelined do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
end
else
@redis.pipelined
end
else
@redis.pipelined
end
end

View File

@ -52,7 +52,39 @@ describe DiscourseRedis do
expect(redis.get('foo')).to eq("baz")
expect(redis.get('baz')).to eq("1")
end
it 'should noop pipelined commands against a readonly redis' do
redis.without_namespace
.expects(:pipelined)
.raises(Redis::CommandError.new("READONLY"))
set, incr = nil
val = redis.pipelined do |pipeline|
set = pipeline.set "foo", "baz"
incr = pipeline.incr "baz"
end
expect(val).to eq(nil)
expect(redis.get('foo')).to eq(nil)
expect(redis.get('baz')).to eq(nil)
end
it 'should noop multi commands against a readonly redis' do
redis.without_namespace
.expects(:multi)
.raises(Redis::CommandError.new("READONLY"))
val = redis.multi do |transaction|
transaction.set 'foo', 'bar'
transaction.set 'bar', 'foo'
transaction.get 'bar'
end
expect(val).to eq(nil)
expect(redis.get('foo')).to eq(nil)
expect(redis.get('bar')).to eq(nil)
end
end