class Mongo::Address
Represents an address to a server, either with an IP address or socket path.
@since 2.0.0
Constants
- FAMILY_MAP
Mapping from socket family to resolver class.
@since 2.0.0
- LOCALHOST
The localhost constant.
@since 2.1.0
Attributes
@return [ String ] host The original host name.
@return [ Integer ] port The port.
@return [ String ] seed The seed address.
Public Class Methods
Initialize the address.
@example Initialize the address with a DNS entry and port.
Mongo::Address.new("app.example.com:27017")
@example Initialize the address with a DNS entry and no port.
Mongo::Address.new("app.example.com")
@example Initialize the address with an IPV4 address and port.
Mongo::Address.new("127.0.0.1:27017")
@example Initialize the address with an IPV4 address and no port.
Mongo::Address.new("127.0.0.1")
@example Initialize the address with an IPV6 address and port.
Mongo::Address.new("[::1]:27017")
@example Initialize the address with an IPV6 address and no port.
Mongo::Address.new("[::1]")
@example Initialize the address with a unix socket.
Mongo::Address.new("/path/to/socket.sock")
@param [ String ] seed The provided address. @param [ Hash ] options The address options.
@since 2.0.0
# File lib/mongo/address.rb, line 70 def initialize(seed, options = {}) if seed.nil? raise ArgumentError, "address must be not nil" end @seed = seed @host, @port = parse_host_port @options = options end
Public Instance Methods
Check equality of the address to another.
@example Check address equality.
address == other
@param [ Object ] other The other object.
@return [ true, false ] If the objects are equal.
@since 2.0.0
# File lib/mongo/address.rb, line 98 def ==(other) return false unless other.is_a?(Address) host == other.host && port == other.port end
@api private
# File lib/mongo/address.rb, line 186 def connect_timeout @connect_timeout ||= @options[:connect_timeout] || Server::CONNECT_TIMEOUT end
Check equality for hashing.
@example Check hashing equality.
address.eql?(other)
@param [ Object ] other The other object.
@return [ true, false ] If the objects are equal.
@since 2.2.0
# File lib/mongo/address.rb, line 113 def eql?(other) self == other end
Calculate the hash value for the address.
@example Calculate the hash value.
address.hash
@return [ Integer ] The hash value.
@since 2.0.0
# File lib/mongo/address.rb, line 125 def hash [ host, port ].hash end
Get a pretty printed address inspection.
@example Get the address inspection.
address.inspect
@return [ String ] The nice inspection string.
@since 2.0.0
# File lib/mongo/address.rb, line 137 def inspect "#<Mongo::Address:0x#{object_id} address=#{to_s}>" end
Get a socket for the provided address, given the options.
The address the socket connects to is determined by the algorithm described in the intialize_resolver! documentation. Each time this method is called, initialize_resolver! will be called, meaning that a new hostname lookup will occur. This is done so that any changes to which addresses the hostname resolves to will be picked up even if a socket has been connected to it before.
@example Get a socket.
address.socket(5, :ssl => true)
@param [ Float ] socket_timeout The socket timeout. @param [ Hash ] ssl_options SSL options. @param [ Hash ] options The options.
@option options [ Float ] :connect_timeout Connect timeout.
@return [ Mongo::Socket::SSL
, Mongo::Socket::TCP
, Mongo::Socket::Unix
] The socket.
@since 2.0.0
# File lib/mongo/address.rb, line 161 def socket(socket_timeout, ssl_options = {}, options = {}) create_resolver(ssl_options).socket(socket_timeout, ssl_options, options) end
Get the address as a string.
@example Get the address as a string.
address.to_s
@return [ String ] The nice string.
@since 2.0.0
# File lib/mongo/address.rb, line 173 def to_s if port if host.include?(':') "[#{host}]:#{port}" else "#{host}:#{port}" end else host end end
Private Instance Methods
To determine which address the socket will connect to, the driver will attempt to connect to each IP address returned by Socket::getaddrinfo in sequence. Once a successful connection is made, a resolver with that IP address specified is returned. If no successful connection is made, the error made by the last connection attempt is raised.
# File lib/mongo/address.rb, line 197 def create_resolver(ssl_options) return Unix.new(seed.downcase) if seed.downcase =~ Unix::MATCH family = (host == LOCALHOST) ? ::Socket::AF_INET : ::Socket::AF_UNSPEC error = nil ::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM).each do |info| begin specific_address = FAMILY_MAP[info[4]].new(info[3], port, host) socket = specific_address.socket( connect_timeout, ssl_options, connect_timeout: connect_timeout) socket.close return specific_address rescue IOError, SystemCallError, Error::SocketTimeoutError, Error::SocketError => e error = e end end raise error end
# File lib/mongo/address.rb, line 216 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