File: transaction_adapter.rb

package info (click to toggle)
ruby-redis-clustering 5.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100 kB
  • sloc: ruby: 256; makefile: 7
file content (104 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

require 'redis_client/cluster/transaction'

class Redis
  class Cluster
    class TransactionAdapter
      class Internal < RedisClient::Cluster::Transaction
        def initialize(client, router, command_builder, node: nil, slot: nil, asking: false)
          @client = client
          super(router, command_builder, node: node, slot: slot, asking: asking)
        end

        def multi
          raise(Redis::Cluster::TransactionConsistencyError, "Can't nest multi transaction")
        end

        def exec
          # no need to do anything
        end

        def discard
          # no need to do anything
        end

        def watch(*_)
          raise(Redis::Cluster::TransactionConsistencyError, "Can't use watch in a transaction")
        end

        def unwatch
          # no need to do anything
        end

        private

        def method_missing(name, *args, **kwargs, &block)
          return call(name, *args, **kwargs, &block) if @client.respond_to?(name)

          super
        end

        def respond_to_missing?(name, include_private = false)
          return true if @client.respond_to?(name)

          super
        end
      end

      def initialize(client, router, command_builder, node: nil, slot: nil, asking: false)
        @client = client
        @router = router
        @command_builder = command_builder
        @node = node
        @slot = slot
        @asking = asking
        @lock_released = false
      end

      def lock_released?
        @lock_released
      end

      def multi
        @lock_released = true
        transaction = Redis::Cluster::TransactionAdapter::Internal.new(
          @client, @router, @command_builder, node: @node, slot: @slot, asking: @asking
        )
        yield transaction
        transaction.execute
      end

      def exec
        # no need to do anything
      end

      def discard
        # no need to do anything
      end

      def watch(*_)
        raise(Redis::Cluster::TransactionConsistencyError, "Can't nest watch command if you use the cluster client")
      end

      def unwatch
        @lock_released = true
        @node.call('UNWATCH')
      end

      private

      def method_missing(name, *args, **kwargs, &block)
        return @client.public_send(name, *args, **kwargs, &block) if @client.respond_to?(name)

        super
      end

      def respond_to_missing?(name, include_private = false)
        return true if @client.respond_to?(name)

        super
      end
    end
  end
end