Class: RedisClient::Cluster::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/transaction.rb

Constant Summary collapse

ConsistencyError =
Class.new(::RedisClient::Error)
MAX_REDIRECTION =
2

Instance Method Summary collapse

Constructor Details

#initialize(router, command_builder, node: nil, resharding: false) ⇒ Transaction

Returns a new instance of Transaction.



13
14
15
16
17
18
19
20
21
22
# File 'lib/redis_client/cluster/transaction.rb', line 13

def initialize(router, command_builder, node: nil, resharding: false)
  @router = router
  @command_builder = command_builder
  @retryable = true
  @pipeline = ::RedisClient::Pipeline.new(@command_builder)
  @pending_commands = []
  @node = node
  prepare_tx unless @node.nil?
  @resharding_state = resharding
end

Instance Method Details

#call(*command, **kwargs, &block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/redis_client/cluster/transaction.rb', line 24

def call(*command, **kwargs, &block)
  command = @command_builder.generate(command, kwargs)
  if prepare(command)
    @pipeline.call_v(command, &block)
  else
    defer { @pipeline.call_v(command, &block) }
  end
end

#call_once(*command, **kwargs, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/redis_client/cluster/transaction.rb', line 42

def call_once(*command, **kwargs, &block)
  @retryable = false
  command = @command_builder.generate(command, kwargs)
  if prepare(command)
    @pipeline.call_once_v(command, &block)
  else
    defer { @pipeline.call_once_v(command, &block) }
  end
end

#call_once_v(command, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/redis_client/cluster/transaction.rb', line 52

def call_once_v(command, &block)
  @retryable = false
  command = @command_builder.generate(command)
  if prepare(command)
    @pipeline.call_once_v(command, &block)
  else
    defer { @pipeline.call_once_v(command, &block) }
  end
end

#call_v(command, &block) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/redis_client/cluster/transaction.rb', line 33

def call_v(command, &block)
  command = @command_builder.generate(command)
  if prepare(command)
    @pipeline.call_v(command, &block)
  else
    defer { @pipeline.call_v(command, &block) }
  end
end

#executeObject

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
# File 'lib/redis_client/cluster/transaction.rb', line 62

def execute
  @pending_commands.each(&:call)

  raise ArgumentError, 'empty transaction' if @pipeline._empty?
  raise ConsistencyError, "couldn't determine the node: #{@pipeline._commands}" if @node.nil?

  settle
end