Class: Mongo::Address

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/address.rb,
lib/mongo/address/ipv4.rb,
lib/mongo/address/ipv6.rb,
lib/mongo/address/unix.rb

Overview

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

Since:

  • 2.0.0

Defined Under Namespace

Classes: IPv4, IPv6, Unix

Constant Summary collapse

FAMILY_MAP =

Mapping from socket family to resolver class.

Since:

  • 2.0.0

{
  ::Socket::PF_UNIX => Unix,
  ::Socket::AF_INET6 => IPv6,
  ::Socket::AF_INET => IPv4
}.freeze
LOCALHOST =

The localhost constant.

Since:

  • 2.1.0

'localhost'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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.

Since:

  • 2.0.0



69
70
71
72
73
# File 'lib/mongo/address.rb', line 69

def initialize(seed, options = {})
  @seed = seed
  @host, @port = parse_host_port
  @options = options
end

Instance Attribute Details

#hostString (readonly)

Returns host The original host name.

Returns:

  • (String)

    host The original host name.

Since:

  • 2.0.0



79
80
81
# File 'lib/mongo/address.rb', line 79

def host
  @host
end

#portInteger (readonly)

Returns port The port.

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0



82
83
84
# File 'lib/mongo/address.rb', line 82

def port
  @port
end

#seedString (readonly)

Returns seed The seed address.

Returns:

  • (String)

    seed The seed address.

Since:

  • 2.0.0



76
77
78
# File 'lib/mongo/address.rb', line 76

def seed
  @seed
end

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



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

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

#connect_timeoutObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



182
183
184
# File 'lib/mongo/address.rb', line 182

def connect_timeout
  @connect_timeout ||= @options[:connect_timeout] || Server::CONNECT_TIMEOUT
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



109
110
111
# File 'lib/mongo/address.rb', line 109

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



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

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



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

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

#socket(socket_timeout, ssl_options = {}, options = {}) ⇒ Mongo::Socket::SSL, ...

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.

Examples:

Get a socket.

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

Parameters:

  • socket_timeout (Float)

    The socket timeout.

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

    SSL options.

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

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

Returns:

Since:

  • 2.0.0



157
158
159
# File 'lib/mongo/address.rb', line 157

def socket(socket_timeout, ssl_options = {}, options = {})
  create_resolver(ssl_options).socket(socket_timeout, ssl_options, options)
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



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mongo/address.rb', line 169

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