Class: RedisClient::Cluster::OptimisticLocking

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

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ OptimisticLocking

Returns a new instance of OptimisticLocking.



9
10
11
12
# File 'lib/redis_client/cluster/optimistic_locking.rb', line 9

def initialize(router)
  @router = router
  @asking = false
end

Instance Method Details

#watch(keys) ⇒ Object



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
# File 'lib/redis_client/cluster/optimistic_locking.rb', line 14

def watch(keys)
  slot = find_slot(keys)
  raise ::RedisClient::Cluster::Transaction::ConsistencyError, "unsafe watch: #{keys.join(' ')}" if slot.nil?

  # We have not yet selected a node for this transaction, initially, which means we can handle
  # redirections freely initially (i.e. for the first WATCH call)
  node = @router.find_primary_node_by_slot(slot)
  handle_redirection(node, retry_count: 1) do |nd|
    nd.with do |c|
      c.ensure_connected_cluster_scoped(retryable: false) do
        c.call('ASKING') if @asking
        c.call('WATCH', *keys)
        begin
          yield(c, slot, @asking)
        rescue ::RedisClient::ConnectionError
          # No need to unwatch on a connection error.
          raise
        rescue StandardError
          c.call('UNWATCH')
          raise
        end
      end
    end
  end
end