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,6 +151,7 @@ class DiscourseRedis
end
def multi
DiscourseRedis.ignore_readonly do
if block_given?
@redis.multi do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
@ -159,8 +160,10 @@ class DiscourseRedis
@redis.multi
end
end
end
def pipelined
DiscourseRedis.ignore_readonly do
if block_given?
@redis.pipelined do |transaction|
yield DiscourseRedis.new(@config, namespace: @namespace, raw_redis: transaction)
@ -169,6 +172,7 @@ class DiscourseRedis
@redis.pipelined
end
end
end
private

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