Class: Moped::Address
- Inherits:
-
Object
- Object
- Moped::Address
- Defined in:
- lib/moped/address.rb
Overview
Encapsulates behaviour around addresses and resolving dns.
Instance Attribute Summary collapse
-
#host ⇒ String
The host name.
-
#ip ⇒ String
The ip address.
-
#original ⇒ String
The original host name.
-
#port ⇒ Integer
The port.
- #resolved ⇒ Object
Instance Method Summary collapse
-
#initialize(address, timeout) ⇒ Address
constructor
Instantiate the new address.
-
#resolve(node) ⇒ String
Resolve the address for the provided node.
Constructor Details
#initialize(address, timeout) ⇒ Address
Instantiate the new address.
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
#host ⇒ String
Returns The host name.
19 20 21 |
# File 'lib/moped/address.rb', line 19 def host @host end |
#ip ⇒ String
Returns The ip address.
19 |
# File 'lib/moped/address.rb', line 19 attr_reader :host, :ip, :original, :port, :resolved |
#original ⇒ String
Returns The original host name.
19 |
# File 'lib/moped/address.rb', line 19 attr_reader :host, :ip, :original, :port, :resolved |
#port ⇒ Integer
Returns The port.
19 |
# File 'lib/moped/address.rb', line 19 attr_reader :host, :ip, :original, :port, :resolved |
#resolved ⇒ Object
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.
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 |