Class: Moped::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/moped/address.rb

Overview

Encapsulates behaviour around addresses and resolving dns.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, timeout) ⇒ Address

Instantiate the new address.

Examples:

Instantiate the address.

Moped::Address.new("localhost:27017")

Parameters:

  • address (String)

    The host:port pair as a string.

Since:

  • 2.0.0



29
30
31
32
33
34
# File 'lib/moped/address.rb', line 29

def initialize(address, timeout)
  @original = address
  @host, port = address.split(":")
  @port = (port || 27017).to_i
  @timeout = timeout
end

Instance Attribute Details

#hostString

Returns The host name.

Returns:

  • (String)

    The host name.



19
20
21
# File 'lib/moped/address.rb', line 19

def host
  @host
end

#ipString

Returns The ip address.

Returns:

  • (String)

    The ip address.



19
# File 'lib/moped/address.rb', line 19

attr_reader :host, :ip, :original, :port, :resolved

#originalString

Returns The original host name.

Returns:

  • (String)

    The original host name.



19
# File 'lib/moped/address.rb', line 19

attr_reader :host, :ip, :original, :port, :resolved

#portInteger

Returns The port.

Returns:

  • (Integer)

    The port.



19
# File 'lib/moped/address.rb', line 19

attr_reader :host, :ip, :original, :port, :resolved

#resolvedObject

Since:

  • 2.0.0



19
# File 'lib/moped/address.rb', line 19

attr_reader :host, :ip, :original, :port, :resolved

Instance Method Details

#resolve(node) ⇒ String

Resolve the address for the provided node. If the address cannot be resolved the node will be flagged as down.

Examples:

Resolve the address.

address.resolve(node)

Parameters:

  • node (Node)

    The node to resolve for.

Returns:

  • (String)

    The resolved address.

Since:

  • 2.0.0



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/moped/address.rb', line 47

def resolve(node)
  begin
    return @resolved if @resolved
    Timeout::timeout(@timeout) do
      Resolv.each_address(host) do |ip|
        if ip =~ Resolv::IPv4::Regex
          @ip ||= ip
          break
        end
      end
      raise Resolv::ResolvError unless @ip
    end
    @resolved = "#{ip}:#{port}"
  rescue Timeout::Error, Resolv::ResolvError, SocketError
    Loggable.warn("  MOPED:", "Could not resolve IP for: #{original}", "n/a")
    node.down! and false
  end
end