123456789_123456789_123456789_123456789_123456789_

Class: Redis::Client

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, RedisClient
Instance Chain:
self, RedisClient
Inherits: RedisClient
  • Object
Defined in: lib/redis/client.rb

Constant Summary

Class Method Summary

Instance Method Summary

Class Method Details

.config(protocol: 3, **kwargs)

[ GitHub ]

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

def config(protocol: 3, **kwargs)
  super(protocol: protocol, **kwargs,
        driver_info: Redis::LibIdentity.driver_info(kwargs[:driver_info]))
end

.resp3_unsupported?(error) ⇒ Boolean

Whether error raised during the connection handshake means the server can't speak RESP3 (so we should retry the connection as RESP2). Covers Redis < 6 (no HELLO command, surfaced by redis-client as UnsupportedServer) and any server replying NOPROTO to HELLO 3.

[ GitHub ]

  
# File 'lib/redis/client.rb', line 42

def resp3_unsupported?(error)
  return true if error.is_a?(::RedisClient::UnsupportedServer)
  return true if error.is_a?(::RedisClient::CommandError) && error.message.include?("NOPROTO")

  # Redis::Cluster discovers its topology by connecting to each startup node. When those nodes
  # don't speak RESP3, redis-cluster-client collects the per-node failures and re-raises them
  # wrapped in an InitialSetupError, discarding the original error classes (see
  # RedisClient::Cluster::InitialSetupError.from_errors). Only the concatenated message
  # survives, so match it to still trigger the RESP2 fallback for pre-6.0 clusters. Guarded by
  # defined? because the cluster error class is only loaded with the redis-clustering gem.
  defined?(::RedisClient::Cluster::InitialSetupError) &&
    error.is_a?(::RedisClient::Cluster::InitialSetupError) &&
    (error.message.include?("NOPROTO") || error.message.include?("HELLO command"))
end

.sentinel(protocol: 3, **kwargs)

[ GitHub ]

  
# File 'lib/redis/client.rb', line 28

def sentinel(protocol: 3, **kwargs)
  super(protocol: protocol, **kwargs,
        driver_info: Redis::LibIdentity.driver_info(kwargs[:driver_info]),
        client_implementation: ::RedisClient)
end

.translate_error!(error, mapping: ERROR_MAPPING)

Raises:

  • (redis_error)
[ GitHub ]

  
# File 'lib/redis/client.rb', line 34

def translate_error!(error, mapping: ERROR_MAPPING)
  redis_error = translate_error_class(error.class, mapping: mapping)
  raise redis_error, error.message, error.backtrace
end

.translate_error_class(error_class, mapping: ERROR_MAPPING) (private)

[ GitHub ]

  
# File 'lib/redis/client.rb', line 59

def translate_error_class(error_class, mapping: ERROR_MAPPING)
  mapping.fetch(error_class)
rescue IndexError
  if (client_error = error_class.ancestors.find { |a| mapping[a] })
    mapping[error_class] = mapping[client_error]
  else
    raise
  end
end

Instance Method Details

#blocking_call_v(timeout, command, &block)

[ GitHub ]

  
# File 'lib/redis/client.rb', line 128

def blocking_call_v(timeout, command, &block)
  if timeout && timeout > 0
    # Can't use the command timeout argument as the connection timeout
    # otherwise it would be very racy. So we add the regular read_timeout on top
    # to account for the network delay.
    timeout += config.read_timeout
  end

  super(timeout, command, &block)
rescue ::RedisClient::Error => error
  raise if Client.resp3_unsupported?(error)

  Client.translate_error!(error)
end

#call_v(command, &block)

We default to RESP3. Servers that can't speak it reject the HELLO 3 handshake (Redis < 6.0 has no HELLO at all; others answer NOPROTO). Re-raise those errors untranslated so they reach Redis#with_protocol_fallback as RedisClient::Error and trigger a transparent rebuild as RESP2 — the same path the sentinel and cluster clients already use. Everything else is translated to the matching Redis::* error.

[ GitHub ]

  
# File 'lib/redis/client.rb', line 120

def call_v(command, &block)
  super(command, &block)
rescue ::RedisClient::Error => error
  raise if Client.resp3_unsupported?(error)

  Client.translate_error!(error)
end

#db

[ GitHub ]

  
# File 'lib/redis/client.rb', line 82

def db
  config.db
end

#host

[ GitHub ]

  
# File 'lib/redis/client.rb', line 90

def host
  config.host unless config.path
end

#id

[ GitHub ]

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

def id
  config.id
end

#inherit_socket!

[ GitHub ]

  
# File 'lib/redis/client.rb', line 159

def inherit_socket!
  @inherit_socket = true
end

#multi(watch: nil)

[ GitHub ]

  
# File 'lib/redis/client.rb', line 151

def multi(watch: nil)
  super
rescue ::RedisClient::Error => error
  raise if Client.resp3_unsupported?(error)

  Client.translate_error!(error)
end

#password

[ GitHub ]

  
# File 'lib/redis/client.rb', line 106

def password
  config.password
end

#path

[ GitHub ]

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

def path
  config.path
end

#pipelined(exception: true)

[ GitHub ]

  
# File 'lib/redis/client.rb', line 143

def pipelined(exception: true)
  super
rescue ::RedisClient::Error => error
  raise if Client.resp3_unsupported?(error)

  Client.translate_error!(error)
end

#port

[ GitHub ]

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

def port
  config.port unless config.path
end

#protocol

[ GitHub ]

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

def protocol
  config.protocol
end

#server_url

[ GitHub ]

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

def server_url
  config.server_url
end

#timeout

[ GitHub ]

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

def timeout
  config.read_timeout
end

#username

[ GitHub ]

  
# File 'lib/redis/client.rb', line 102

def username
  config.username
end