Class: Redis::Connection::SSLSocket

Inherits:
OpenSSL::SSL::SSLSocket
  • Object
show all
Includes:
SocketMixin
Defined in:
lib/redis/connection/ruby.rb

Constant Summary

Constants included from SocketMixin

Redis::Connection::SocketMixin::CRLF

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SocketMixin

#_read_from_socket, #_write_to_socket, #gets, #initialize, #read, #timeout=, #write, #write_timeout=

Class Method Details

.connect(host, port, timeout, ssl_params) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/redis/connection/ruby.rb', line 252

def self.connect(host, port, timeout, ssl_params)
  # Note: this is using Redis::Connection::TCPSocket
  tcp_sock = TCPSocket.connect(host, port, timeout)

  ctx = OpenSSL::SSL::SSLContext.new

  # The provided parameters are merged into OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
  ctx.set_params(ssl_params || {})

  ssl_sock = new(tcp_sock, ctx)
  ssl_sock.hostname = host

  begin
    # Initiate the socket connection in the background. If it doesn't fail
    # immediately it will raise an IO::WaitWritable (Errno::EINPROGRESS)
    # indicating the connection is in progress.
    # Unlike waiting for a tcp socket to connect, you can't time out ssl socket
    # connections during the connect phase properly, because IO.select only partially works.
    # Instead, you have to retry.
    ssl_sock.connect_nonblock
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
    if ssl_sock.wait_readable(timeout)
      retry
    else
      raise TimeoutError
    end
  rescue IO::WaitWritable
    if ssl_sock.wait_writable(timeout)
      retry
    else
      raise TimeoutError
    end
  end

  unless ctx.verify_mode == OpenSSL::SSL::VERIFY_NONE || (
    ctx.respond_to?(:verify_hostname) &&
    !ctx.verify_hostname
  )
    ssl_sock.post_connection_check(host)
  end

  ssl_sock
end

Instance Method Details

#wait_readable(timeout = nil) ⇒ Object



241
242
243
# File 'lib/redis/connection/ruby.rb', line 241

def wait_readable(timeout = nil)
  to_io.wait_readable(timeout)
end

#wait_writable(timeout = nil) ⇒ Object



247
248
249
# File 'lib/redis/connection/ruby.rb', line 247

def wait_writable(timeout = nil)
  to_io.wait_writable(timeout)
end