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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/moped/address.rb', line 47

def resolve(node)
  return @resolved if @resolved
  start = Time.now
  retries = 0
  begin
    # This timeout should be very large since Timeout::timeout plays very badly with multithreaded code
    # TODO: Remove this Timeout entirely
    Timeout::timeout(@timeout * 10) 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 => e
    msg = ["  MOPED:", "Could not resolve IP for: #{original}, delta is #{Time.now - start}, error class is #{e.inspect}, retries is #{retries}. Node is #{node.inspect}", "n/a"]
    if retries == 0
      Loggable.info(*msg)
    else
      Loggable.warn(*msg)
    end
    if retries < 2
      retries += 1
      retry
    else
      node.down! and false
    end
  end
end