Skip to content

Commit 2d6d7ff

Browse files
authored
Merge pull request #747 from chanks/optimize-empty-transactions
Optimize empty transactions
2 parents 6efb53a + 580449b commit 2d6d7ff

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/redis/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@ def call_loop(command, timeout = 0)
150150
end
151151

152152
def call_pipeline(pipeline)
153+
commands = pipeline.commands
154+
return [] if commands.empty?
155+
153156
with_reconnect pipeline.with_reconnect? do
154157
begin
155-
pipeline.finish(call_pipelined(pipeline.commands)).tap do
158+
pipeline.finish(call_pipelined(commands)).tap do
156159
self.db = pipeline.db if pipeline.db
157160
end
158161
rescue ConnectionError => e

lib/redis/pipeline.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def shutdown?
2222
@shutdown
2323
end
2424

25+
def empty?
26+
@futures.empty?
27+
end
28+
2529
def call(command, &block)
2630
# A pipeline that contains a shutdown should not raise ECONNRESET when
2731
# the connection is gone.
@@ -86,7 +90,11 @@ def finish(replies)
8690
end
8791

8892
def commands
89-
[[:multi]] + super + [[:exec]]
93+
if empty?
94+
[]
95+
else
96+
[[:multi]] + super + [[:exec]]
97+
end
9098
end
9199
end
92100
end

test/transactions_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def test_raise_command_errors_in_multi_exec
114114
assert_equal "s1", r.get("foo")
115115
end
116116

117+
def test_empty_multi_exec
118+
result = nil
119+
120+
redis_mock(:exec => lambda { |*_| "-ERROR" }) do |redis|
121+
result = redis.multi {}
122+
end
123+
124+
assert_equal [], result
125+
end
126+
117127
def test_raise_command_errors_when_accessing_futures_after_multi_exec
118128
begin
119129
r.multi do |m|

0 commit comments

Comments
 (0)