Class: Redis::Cluster::Client

Inherits:
RedisClient::Cluster
  • Object
show all
Defined in:
lib/redis/cluster/client.rb

Constant Summary collapse

ERROR_MAPPING =
::Redis::Client::ERROR_MAPPING.merge(
  RedisClient::Cluster::InitialSetupError => Redis::Cluster::InitialSetupError,
  RedisClient::Cluster::OrchestrationCommandNotSupported => Redis::Cluster::OrchestrationCommandNotSupported,
  RedisClient::Cluster::AmbiguousNodeError => Redis::Cluster::AmbiguousNodeError,
  RedisClient::Cluster::ErrorCollection => Redis::Cluster::CommandErrorCollection,
  RedisClient::Cluster::Transaction::ConsistencyError => Redis::Cluster::TransactionConsistencyError,
  RedisClient::Cluster::NodeMightBeDown => Redis::Cluster::NodeMightBeDown,
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



51
52
53
# File 'lib/redis/cluster/client.rb', line 51

def initialize(*)
  handle_errors { super }
end

Class Method Details

.config(**kwargs) ⇒ Object



19
20
21
# File 'lib/redis/cluster/client.rb', line 19

def config(**kwargs)
  super(protocol: 2, **kwargs)
end

.sentinel(**kwargs) ⇒ Object



23
24
25
# File 'lib/redis/cluster/client.rb', line 23

def sentinel(**kwargs)
  super(protocol: 2, **kwargs)
end

.translate_error!(error, mapping: ERROR_MAPPING) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redis/cluster/client.rb', line 27

def translate_error!(error, mapping: ERROR_MAPPING)
  case error
  when RedisClient::Cluster::ErrorCollection
    error.errors.each do |_node, node_error|
      if node_error.is_a?(RedisClient::AuthenticationError)
        raise mapping.fetch(node_error.class), node_error.message, node_error.backtrace
      end
    end

    remapped_node_errors = error.errors.map do |node_key, node_error|
      remapped = mapping.fetch(node_error.class, node_error.class).new(node_error.message)
      remapped.set_backtrace node_error.backtrace
      [node_key, remapped]
    end.to_h

    raise(Redis::Cluster::CommandErrorCollection.new(remapped_node_errors, error.message).tap do |remapped|
      remapped.set_backtrace error.backtrace
    end)
  else
    Redis::Client.translate_error!(error, mapping: mapping)
  end
end

Instance Method Details

#blocking_call_v(timeout, command, &block) ⇒ Object



89
90
91
92
# File 'lib/redis/cluster/client.rb', line 89

def blocking_call_v(timeout, command, &block)
  timeout += self.timeout if timeout && timeout > 0
  handle_errors { super(timeout, command, &block) }
end

#call_v(command, &block) ⇒ Object



85
86
87
# File 'lib/redis/cluster/client.rb', line 85

def call_v(command, &block)
  handle_errors { super(command, &block) }
end

#connected?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/redis/cluster/client.rb', line 64

def connected?
  true
end

#dbObject



76
77
78
# File 'lib/redis/cluster/client.rb', line 76

def db
  0
end

#disable_reconnectionObject



68
69
70
# File 'lib/redis/cluster/client.rb', line 68

def disable_reconnection
  yield # TODO: do we need this, is it doable?
end

#idObject



56
57
58
# File 'lib/redis/cluster/client.rb', line 56

def id
  server_url.join(' ')
end

#multi(watch: nil, &block) ⇒ Object



98
99
100
# File 'lib/redis/cluster/client.rb', line 98

def multi(watch: nil, &block)
  handle_errors { super(watch: watch, &block) }
end

#pipelined(exception: true, &block) ⇒ Object



94
95
96
# File 'lib/redis/cluster/client.rb', line 94

def pipelined(exception: true, &block)
  handle_errors { super(exception: exception, &block) }
end

#server_urlObject



60
61
62
# File 'lib/redis/cluster/client.rb', line 60

def server_url
  @router.nil? ? @config.startup_nodes.keys : router.node_keys
end

#timeoutObject



72
73
74
# File 'lib/redis/cluster/client.rb', line 72

def timeout
  config.read_timeout
end

#watch(*keys, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/redis/cluster/client.rb', line 102

def watch(*keys, &block)
  unless block_given?
    raise(
      Redis::Cluster::TransactionConsistencyError,
      'A block is required if you use the cluster client.'
    )
  end

  unless block.arity == 1
    raise(
      Redis::Cluster::TransactionConsistencyError,
      'Given block needs an argument if you use the cluster client.'
    )
  end

  handle_errors do
    RedisClient::Cluster::OptimisticLocking.new(router).watch(keys) do |c, slot, asking|
      transaction = Redis::Cluster::TransactionAdapter.new(
        self, router, @command_builder, node: c, slot: slot, asking: asking
      )

      result = yield transaction
      c.call('UNWATCH') unless transaction.lock_released?
      result
    end
  end
end