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.
Constant Summary
-
FAMILY_MAP =
# File 'lib/mongo/address.rb', line 33
Mapping from socket family to resolver class.
{ ::Socket::PF_UNIX => Unix, ::Socket::AF_INET6 => IPv6, ::Socket::AF_INET => IPv4 }.freeze -
LOCALHOST =
# File 'lib/mongo/address.rb', line 42
The localhost constant.
'localhost'
Class Method Summary
-
.new(seed, options = {}) ⇒ Address
constructor
Initialize the address.
Instance Attribute Summary
- #host ⇒ String readonly
- #options readonly Internal use only Internal use only
- #port ⇒ Integer readonly
- #seed ⇒ String readonly
Instance Method Summary
-
#==(other) ⇒ true, false
Check equality of the address to another.
-
#eql?(other) ⇒ true, false
Check equality for hashing.
-
#hash ⇒ Integer
Calculate the hash value for the address.
-
#inspect ⇒ String
Get a pretty printed address inspection.
-
#socket(socket_timeout, opts = {}) ⇒ Mongo::Socket::SSL | Mongo::Socket::TCP | Mongo::Socket::Unix
Internal use only
Internal use only
Get a socket for the address stored in this object, given the options.
-
#to_s ⇒ String
Get the address as a string.
-
#getaddrinfo(host, family)
private
This is a simple wrapper around
Socket.getaddrinfo added to make testing easier. -
#map_exceptions(csot)
private
Maps some errors to different ones, mostly low-level errors to driver level errors.
- #parse_host_port private
Constructor Details
.new(seed, options = {}) ⇒ Address
Initialize the address.
# File 'lib/mongo/address.rb', line 73
def initialize(seed, = {}) raise ArgumentError, 'address must be not nil' if seed.nil? @seed = seed @host, @port = parse_host_port @options = Hash[.map { |k, v| [ k.to_sym, v ] }] end
Instance Attribute Details
#host ⇒ String (readonly)
# File 'lib/mongo/address.rb', line 85
attr_reader :host
#options (readonly)
# File 'lib/mongo/address.rb', line 91
attr_reader :
#port ⇒ Integer (readonly)
# File 'lib/mongo/address.rb', line 88
attr_reader :port
#seed ⇒ String (readonly)
# File 'lib/mongo/address.rb', line 82
attr_reader :seed
Instance Method Details
#==(other) ⇒ true, false
Check equality of the address to another.
#eql?(other) ⇒ true, false
Check equality for hashing.
# File 'lib/mongo/address.rb', line 119
def eql?(other) self == other end
#getaddrinfo(host, family) (private)
This is a simple wrapper around Socket.getaddrinfo added to make testing easier.
#hash ⇒ Integer
Calculate the hash value for the address.
#inspect ⇒ String
Get a pretty printed address inspection.
# File 'lib/mongo/address.rb', line 143
def inspect "#<Mongo::Address:0x#{object_id} address=#{self}>" end
#map_exceptions(csot) (private)
Maps some errors to different ones, mostly low-level errors to driver level errors
# File 'lib/mongo/address.rb', line 295
def map_exceptions(csot) yield rescue Errno::ETIMEDOUT => e raise Error::TimeoutError, "#{e.class}: #{e} (for #{self})" if csot raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{self})" rescue Error::SocketTimeoutError => e raise Error::TimeoutError, "#{e.class}: #{e} (for #{self})" if csot raise e rescue IOError, SystemCallError, ::SocketError => e raise Error::SocketError, "#{e.class}: #{e} (for #{self})" rescue OpenSSL::SSL::SSLError => e raise Error::SocketError, "#{e.class}: #{e} (for #{self})" end
#parse_host_port (private)
#socket(socket_timeout, opts = {}) ⇒ Mongo::Socket::SSL | Mongo::Socket::TCP | Mongo::Socket::Unix
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.
# File 'lib/mongo/address.rb', line 217
def socket(socket_timeout, opts = {}) csot = !!opts[:csot] opts = { connect_timeout: Server::CONNECT_TIMEOUT, }.update().update(Hash[opts.map { |k, v| [ k.to_sym, v ] }]) map_exceptions(csot) do if Unix::MATCH.match?(seed.downcase) 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 = getaddrinfo(host, family) results = infos.map do |info| [ info[4], info[3] ] end.uniq results.each do |family, address_str| 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 raise error end end
#to_s ⇒ String
Get the address as a string.