123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Socket::TCP Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Socket
Instance Chain:
self, Socket
Inherits: Socket
  • Object
Defined in: lib/mongo/socket/tcp.rb

Overview

Wrapper for TCP sockets.

Since:

  • 2.0.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)

    host The host to connect to.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 62

attr_reader :host

#portInteger (readonly)

Returns:

  • (Integer)

    port The port to connect to.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 65

attr_reader :port

Instance Method Details

#connect!TCP

This method is for internal use only.
Note:

This method mutates the object by setting the socket internally.

Establishes a socket connection.

Examples:

Connect the socket.

sock.connect!

Returns:

  • (TCP)

    The connected socket instance.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 79

def connect!
  socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
  sockaddr = ::Socket.pack_sockaddr_in(port, host)
  connect_timeout = options[:connect_timeout]
  map_exceptions do
    if connect_timeout && connect_timeout != 0
      connect_with_timeout(sockaddr, connect_timeout)
    else
      connect_without_timeout(sockaddr)
    end
  end
  self
end

#connect_with_timeout(sockaddr, connect_timeout)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 99

def connect_with_timeout(sockaddr, connect_timeout)
  if connect_timeout <= 0
    raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect"
  end

  deadline = Utils.monotonic_time + connect_timeout
  begin
    socket.connect_nonblock(sockaddr)
  rescue IO::WaitWritable
    select_timeout = deadline - Utils.monotonic_time
    if select_timeout <= 0
      raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect"
    end

    if IO.select(nil, [ socket ], nil, select_timeout)
      retry
    else
      socket.close
      raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect"
    end
  rescue Errno::EISCONN
    # Socket is connected, nothing more to do
  end
end

#connect_without_timeout(sockaddr)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 94

def connect_without_timeout(sockaddr)
  socket.connect(sockaddr)
end

#human_address (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 126

def human_address
  "#{host}:#{port} (no TLS)"
end