Skip to content

Commit 26c0592

Browse files
authored
fix: the public methods for the block of the transaction should return nil because of the queueing (#324)
1 parent a0f560f commit 26c0592

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/redis_client/cluster/transaction.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def initialize(router, command_builder, watch)
1515
@watch = watch
1616
@retryable = true
1717
@pipeline = ::RedisClient::Pipeline.new(@command_builder)
18-
@buffer = []
18+
@pending_commands = []
1919
@node = nil
2020
end
2121

@@ -24,7 +24,7 @@ def call(*command, **kwargs, &block)
2424
if prepare(command)
2525
@pipeline.call_v(command, &block)
2626
else
27-
@buffer << -> { @pipeline.call_v(command, &block) }
27+
defer { @pipeline.call_v(command, &block) }
2828
end
2929
end
3030

@@ -33,7 +33,7 @@ def call_v(command, &block)
3333
if prepare(command)
3434
@pipeline.call_v(command, &block)
3535
else
36-
@buffer << -> { @pipeline.call_v(command, &block) }
36+
defer { @pipeline.call_v(command, &block) }
3737
end
3838
end
3939

@@ -43,7 +43,7 @@ def call_once(*command, **kwargs, &block)
4343
if prepare(command)
4444
@pipeline.call_once_v(command, &block)
4545
else
46-
@buffer << -> { @pipeline.call_once_v(command, &block) }
46+
defer { @pipeline.call_once_v(command, &block) }
4747
end
4848
end
4949

@@ -53,12 +53,12 @@ def call_once_v(command, &block)
5353
if prepare(command)
5454
@pipeline.call_once_v(command, &block)
5555
else
56-
@buffer << -> { @pipeline.call_once_v(command, &block) }
56+
defer { @pipeline.call_once_v(command, &block) }
5757
end
5858
end
5959

6060
def execute
61-
@buffer.each(&:call)
61+
@pending_commands.each(&:call)
6262

6363
raise ArgumentError, 'empty transaction' if @pipeline._empty?
6464
raise ConsistencyError, "couldn't determine the node: #{@pipeline._commands}" if @node.nil?
@@ -69,6 +69,11 @@ def execute
6969

7070
private
7171

72+
def defer(&block)
73+
@pending_commands << block
74+
nil
75+
end
76+
7277
def watch?
7378
!@watch.nil? && !@watch.empty?
7479
end
@@ -97,8 +102,8 @@ def prepare(command)
97102
@node = @router.find_node(node_key)
98103
@pipeline.call('WATCH', *@watch) if watch?
99104
@pipeline.call('MULTI')
100-
@buffer.each(&:call)
101-
@buffer.clear
105+
@pending_commands.each(&:call)
106+
@pending_commands.clear
102107
true
103108
end
104109

0 commit comments

Comments
 (0)