Class: RedisClient::RubyConnection

Inherits:
Object
  • Object
show all
Includes:
ConnectionMixin
Defined in:
lib/redis_client/ruby_connection.rb,
lib/redis_client/ruby_connection/buffered_io.rb

Defined Under Namespace

Classes: BufferedIO

Constant Summary collapse

SUPPORTS_RESOLV_TIMEOUT =
Socket.method(:tcp).parameters.any? { |p| p.last == :resolv_timeout }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionMixin

#call, #call_pipelined, #connection_timeout, #reconnect, #revalidate

Constructor Details

#initialize(config, connect_timeout:, read_timeout:, write_timeout:) ⇒ RubyConnection

Returns a new instance of RubyConnection.



45
46
47
48
49
50
51
52
# File 'lib/redis_client/ruby_connection.rb', line 45

def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
  super()
  @config = config
  @connect_timeout = connect_timeout
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  connect
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/redis_client/ruby_connection.rb', line 43

def config
  @config
end

Class Method Details

.ssl_context(ssl_params) ⇒ 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/ruby_connection.rb', line 14

def ssl_context(ssl_params)
  params = ssl_params.dup || {}

  cert = params[:cert]
  if cert.is_a?(String)
    cert = File.read(cert) if File.exist?(cert)
    params[:cert] = OpenSSL::X509::Certificate.new(cert)
  end

  key = params[:key]
  if key.is_a?(String)
    key = File.read(key) if File.exist?(key)
    params[:key] = OpenSSL::PKey.read(key)
  end

  context = OpenSSL::SSL::SSLContext.new
  context.set_params(params)
  if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
    if context.respond_to?(:verify_hostname) # Missing on JRuby
      context.verify_hostname
    end
  end

  context
end

Instance Method Details

#closeObject



58
59
60
61
# File 'lib/redis_client/ruby_connection.rb', line 58

def close
  @io.close
  super
end

#connected?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/redis_client/ruby_connection.rb', line 54

def connected?
  !@io.closed?
end

#measure_round_trip_delayObject



106
107
108
109
110
# File 'lib/redis_client/ruby_connection.rb', line 106

def measure_round_trip_delay
  start = RedisClient.now_ms
  call(["PING"], @read_timeout)
  RedisClient.now_ms - start
end

#read(timeout = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/redis_client/ruby_connection.rb', line 94

def read(timeout = nil)
  if timeout.nil?
    RESP3.load(@io)
  else
    @io.with_timeout(timeout) { RESP3.load(@io) }
  end
rescue RedisClient::RESP3::UnknownType => error
  raise RedisClient::ProtocolError.with_config(error.message, config)
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
  raise ConnectionError.with_config(error.message, config)
end

#read_timeout=(timeout) ⇒ Object



63
64
65
66
# File 'lib/redis_client/ruby_connection.rb', line 63

def read_timeout=(timeout)
  @read_timeout = timeout
  @io.read_timeout = timeout if @io
end

#write(command) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/redis_client/ruby_connection.rb', line 73

def write(command)
  buffer = RESP3.dump(command)
  begin
    @io.write(buffer)
  rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
    raise ConnectionError.with_config(error.message, config)
  end
end

#write_multi(commands) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/redis_client/ruby_connection.rb', line 82

def write_multi(commands)
  buffer = nil
  commands.each do |command|
    buffer = RESP3.dump(command, buffer)
  end
  begin
    @io.write(buffer)
  rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
    raise ConnectionError.with_config(error.message, config)
  end
end

#write_timeout=(timeout) ⇒ Object



68
69
70
71
# File 'lib/redis_client/ruby_connection.rb', line 68

def write_timeout=(timeout)
  @write_timeout = timeout
  @io.write_timeout = timeout if @io
end