123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Address

Relationships & Source Files
Namespace Children
Modules:
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/address.rb,
lib/mongo/address/ipv4.rb,
lib/mongo/address/ipv6.rb,
lib/mongo/address/unix.rb,
lib/mongo/address/validator.rb

Overview

Represents an address to a server, either with an IP address or socket path.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(seed, options = {}) ⇒ Address

Initialize the address.

Examples:

Initialize the address with a DNS entry and port.

Mongo::Address.new("app.example.com:27017")

Initialize the address with a DNS entry and no port.

Mongo::Address.new("app.example.com")

Initialize the address with an IPV4 address and port.

Mongo::Address.new("127.0.0.1:27017")

Initialize the address with an IPV4 address and no port.

Mongo::Address.new("127.0.0.1")

Initialize the address with an IPV6 address and port.

Mongo::Address.new("[::1]:27017")

Initialize the address with an IPV6 address and no port.

Mongo::Address.new("[::1]")

Initialize the address with a unix socket.

Mongo::Address.new("/path/to/socket.sock")

Parameters:

  • seed (String)

    The provided address.

  • options (Hash) (defaults to: {})

    The address options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 75

def initialize(seed, options = {})
  if seed.nil?
    raise ArgumentError, "address must be not nil"
  end
  @seed = seed
  @host, @port = parse_host_port
  @options = Hash[options.map { |k, v| [k.to_sym, v] }]
end

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)

    host The original host name.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 88

attr_reader :host

#options (readonly)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 94

attr_reader :options

#portInteger (readonly)

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 91

attr_reader :port

#seedString (readonly)

Returns:

  • (String)

    seed The seed address.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 85

attr_reader :seed

Instance Method Details

#==(other) ⇒ true, false

Check equality of the address to another.

Examples:

Check address equality.

address == other

Parameters:

  • other (Object)

    The other object.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 106

def ==(other)
  return false unless other.is_a?(Address)
  host == other.host && port == other.port
end

#eql?(other) ⇒ true, false

Check equality for hashing.

Examples:

Check hashing equality.

address.eql?(other)

Parameters:

  • other (Object)

    The other object.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 121

def eql?(other)
  self == other
end

#hashInteger

Calculate the hash value for the address.

Examples:

Calculate the hash value.

address.hash

Returns:

  • (Integer)

    The hash value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 133

def hash
  [ host, port ].hash
end

#inspectString

Get a pretty printed address inspection.

Examples:

Get the address inspection.

address.inspect

Returns:

  • (String)

    The nice inspection string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 145

def inspect
  "#<Mongo::Address:0x#{object_id} address=#{to_s}>"
end

#map_exceptions (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 284

def map_exceptions
  begin
    yield
  rescue Errno::ETIMEDOUT => e
    raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{self})"
  rescue IOError, SystemCallError => e
    raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
  rescue OpenSSL::SSL::SSLError => e
    raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
  end
end

#parse_host_port (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 275

def parse_host_port
  address = seed.downcase
  case address
    when Unix::MATCH then Unix.parse(address)
    when IPv6::MATCH then IPv6.parse(address)
    else IPv4.parse(address)
  end
end

#socket(socket_timeout, opts = {}) ⇒ Mongo::Socket::SSL | Mongo::Socket::TCP | Mongo::Socket::Unix

This method is for internal use only.

Get a socket for the address stored in this object, given the options.

If the address stored in this object looks like a Address::Unix path, this method returns a Address::Unix domain socket for this path.

Otherwise, this method attempts to resolve the address stored in this object to Address::IPv4 and Address::IPv6 addresses using Socket#getaddrinfo, then connects to the resulting addresses and returns the socket of the first successful connection. The order in which address families (IPv4/IPV6) are tried is the same order in which the addresses are returned by getaddrinfo, and is determined by the host system.

Name resolution is performed on each socket call. This is done so that any changes to which addresses the host names used as seeds or in server configuration resolve to are immediately noticed by the driver, even if a socket has been connected to the affected host name/address before. However, note that DNS TTL values may still affect when a change to a host address is noticed by the driver.

This method propagates any exceptions raised during DNS resolution and subsequent connection attempts. In case of a host name resolving to multiple IP addresses, the error raised by the last attempt is propagated to the caller. This method does not map exceptions to Error subclasses, and may raise any subclass of Exception.

Examples:

Get a socket.

address.socket(5, :ssl => true)

Parameters:

  • socket_timeout (Float)

    The socket timeout.

  • opts (Hash) (defaults to: {})

    The options.

Options Hash (opts):

  • :connect_timeout (Float)

    Connect timeout.

  • :ssl (true | false)

    Whether to use SSL.

  • :ssl_ca_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_object (Array<OpenSSL::X509::Certificate>)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_object (OpenSSL::X509::Certificate)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_object (OpenSSL::PKey)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_pass_phrase (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_certificate (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_hostname (true, false)

    Same as the corresponding Client/Socket::SSL option.

Returns:

Raises:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 216

def socket(socket_timeout, opts = {})
  opts = {
    connect_timeout: Server::CONNECT_TIMEOUT,
  }.update(options).update(Hash[opts.map { |k, v| [k.to_sym, v] }])

  map_exceptions do
    if seed.downcase =~ Unix::MATCH
      specific_address = Unix.new(seed.downcase)
      return specific_address.socket(socket_timeout, opts)
    end

    # When the driver connects to "localhost", it only attempts IPv4
    # connections. When the driver connects to other hosts, it will
    # attempt both IPv4 and IPv6 connections.
    family = (host == LOCALHOST) ? ::Socket::AF_INET : ::Socket::AF_UNSPEC
    error = nil
    # Sometimes Socket#getaddrinfo returns the same info more than once
    # (multiple identical items in the returned array). It does not make
    # sense to try to connect to the same address more than once, thus
    # eliminate duplicates here.
    infos = ::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
    results = infos.map do |info|
      [info[4], info[3]]
    end.uniq
    results.each do |family, address_str|
      begin
        specific_address = FAMILY_MAP[family].new(address_str, port, host)
        socket = specific_address.socket(socket_timeout, opts)
        return socket
      rescue IOError, SystemCallError, Error::SocketTimeoutError, Error::SocketError => e
        error = e
      end
    end
    raise error
  end
end

#to_sString

Get the address as a string.

Examples:

Get the address as a string.

address.to_s

Returns:

  • (String)

    The nice string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address.rb', line 261

def to_s
  if port
    if host.include?(':')
      "[#{host}]:#{port}"
    else
      "#{host}:#{port}"
    end
  else
    host
  end
end